如何从ALAsset Url获取视频网址以在服务器上传视频?

时间:2014-02-21 07:33:30

标签: ios objective-c alasset elcimagepickercontroller

使用assets-library://asset/asset.mp4?id=F0D95698-C982-4723-8959-502CE595E3D1&ext=mp4选择视频时,我会收到ELCImagePickerController个网址。现在,我将使用asiDataFormRequest检索用于在服务器上上传此视频的视频名称和媒体网址。

当我使用ImagePickerViewController选择视频时,视频上传工作正常。此时我必须选择多个视频,因此我使用ELCImagePickerController。但它给出了视频网址,如下所示。 assets-library://asset/asset.mp4?id=F0D95698-C982-4723-8959-502CE595E3D1&ext=mp4

如何以MEdia Url类型格式转换此网址。我的主要目的是使用asihttpdatafromrequest上传此视频并获取此尺寸,名称。

1 个答案:

答案 0 :(得分:0)

@KDRocks。此代码已成功运行以获取具有完整路径的名称。

-(NSString*) videoAssetURLToTempFile:(NSURL*)url
{
    NSString * surl = [url absoluteString];
    NSString * ext = [surl substringFromIndex:[surl rangeOfString:@"ext="].location + 4];
    NSTimeInterval ti = [[NSDate date]timeIntervalSinceReferenceDate];
    NSString * filename = [NSString stringWithFormat: @"%f.%@",ti,ext];
    NSString * tmpfile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation * rep = [myasset defaultRepresentation];
        NSUInteger size = [rep size];
        const int bufferSize = 8192;
        NSLog(@"Writing to %@",tmpfile);
        FILE* f = fopen([tmpfile cStringUsingEncoding:1], "wb+");
        if (f == NULL) 
        {
            NSLog(@"Can not create tmp file.");
            return;
        }
        Byte * buffer = (Byte*)malloc(bufferSize);
        int read = 0, offset = 0, written = 0;
        NSError* err;
        if (size != 0) {
        do {
            read = [rep getBytes:buffer
                      fromOffset:offset
                          length:bufferSize
                           error:&err];
            written = fwrite(buffer, sizeof(char), read, f);
            offset += read;
        } while (read != 0);
        }
        fclose(f);
    };
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can not get asset - %@",[myerror localizedDescription]);

    };
    if(url)
    {
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:url
                   resultBlock:resultblock
                  failureBlock:failureblock];
    }
    return tmpfile;
}