我做的是iOS开发。
需要实现的功能: 从手机相册中导入视频文件到我的APP中(后续会对这个文件进行操作).
遇到的障碍:我拿不到相册中的视频文件的实际地址(文件路径),最多只能拿到它在相册中的URL。
求大神帮忙解决下问题,感激不尽。
以下是我的部分代码,我需要再注释处获得文件路径:
-(void)assetPickerController:(ZYQAssetPickerController )picker didFinishPickingAssets:(NSArray )assets{
//TODO:
for (ALAsset *asset in assets) {
NSLog(@"HERE\n asset.defaultRepresentation.url = %@\n asset.defaultRepresentation.filename = %@",asset.defaultRepresentation.url, asset.defaultRepresentation.filename);
[self saveVideoToApp:nil asset:asset];
// 我想在这里拿到视频文件
}
}
以下是我的打印数据
2015-11-13 20:51:57.556 MyApp[1244:499923] HERE
asset.defaultRepresentation.url = assets-library://asset/asset.MOV?id=08A49AFB-8FA6-4F69-824F-A34CB168CAAA&ext=MOV
asset.defaultRepresentation.filename = IMG_0006.MOV
多谢
迷茫2017-04-17 17:30:08
The specific path of the file in the system album cannot be obtained through ALAsset. The general method is to directly obtain the video data through ALAssetRepresentation. If a fileURL is also needed, the video data can be written into a custom fileURL file. Sample code As follows:
-(void)assetPickerController:(ZYQAssetPickerController )picker didFinishPickingAssets:(NSArray )assets{
//TODO:
for (ALAsset *asset in assets) {
NSLog(@"HERE\n asset.defaultRepresentation.url = %@\n asset.defaultRepresentation.filename = %@",asset.defaultRepresentation.url, asset.defaultRepresentation.filename);
[self saveVideoToApp:nil asset:asset];
// 我想在这里拿到视频文件
ALAssetRepresentation *representation = asset.defaultRepresentation;
if ([mediaType isEqualToString:ALAssetTypeVideo]) {
long long size = representation.size;
NSMutableData* data = [[NSMutableData alloc] initWithCapacity:size];
void* buffer = [data mutableBytes];
[representation getBytes:buffer fromOffset:0 length:size error:nil];
NSData *fileData = [[NSData alloc] initWithBytes:buffer length:size];
//File URL
NSString *savePath = [NSString stringWithFormat:@"%@%@", [NSHomeDirectory() stringByAppendingString:@"/tmp/"], representation.filename];
[[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
[fileData writeToFile:savePath atomically:NO];
});
}
}
}
大家讲道理2017-04-17 17:30:08
NSURL *videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *data = [NSData dataWithContentsOfURL:videoUrl];
怪我咯2017-04-17 17:30:08
. . . Why hasn't it been resolved yet?
First you play this file, there is no need to export it at all!
ALAsset *alasset = ...;
AVAsset *avasset = [AVAsset assetWithURL:alasset.defaultRepresentation.url];
This AVAsset can be used for playback, editing, etc.
Then if you need to go anywhere, you can use
AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:avasset presetName:AVAssetExportPresetHighestQuality];
session.outputFileType = AVFileTypeMPEG4;
session.outputURL = ...; // 这个就是你可以导出的文件路径了。
[session exportAsynchronouslyWithCompletionHandler: ...];
Then this method can add a lot of parameters to help you export. For details, you can look at AVComposition, huh.