【问题标题】:How to get video url from ALAsset Url for uploading a video on server?如何从 ALAsset Url 获取视频 url 以在服务器上上传视频?
【发布时间】:2014-02-21 07:33:29
【问题描述】:
当使用 ELCImagePickerController 选择视频时,我收到了 assets-library://asset/asset.mp4?id=F0D95698-C982-4723-8959-502CE595E3D1&ext=mp4 url。现在,我必须使用asiDataFormRequest 检索视频名称和媒体网址,以便将此视频上传到服务器上。
当我使用ImagePickerViewController 选择视频时,视频上传工作正常。现在我必须选择多个视频,所以我使用ELCImagePickerController。但它给出了下面给出的视频网址。
assets-library://asset/asset.mp4?id=F0D95698-C982-4723-8959-502CE595E3D1&ext=mp4
如何将此网址转换为 MEdia Url 类型格式。我的主要目标是使用 asihttpdatafromrequest 上传此视频并获得此大小、名称。
【问题讨论】:
标签:
ios
objective-c
alasset
elcimagepickercontroller
【解决方案1】:
@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;
}