[manager requestAVAssetForVideo:PHAssetoptions:videoRequestOptionsresultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {}];
代码报错:原因是
avasset 可能是AVURLAsset 与 AVComposition , 直接调用 【avasset URL】,当选取的视频 是慢动作视频,huicrash;
解决思路:
1、过滤慢动作视频
//加载视频
+ (void)loadVideos {dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ALAssetsLibrary *videolibrary = [[ALAssetsLibrary alloc] init];__block NSMutableArray *reList=[NSMutableArray array];__block BOOL flag=true;[videolibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {if (group) {[group setAssetsFilter:[ALAssetsFilter allVideos]];[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {if (result) {BDHKUGCVideoInfoItem *videoInfo=[[BDHKUGCVideoInfoItem alloc] init];videoInfo.thumbnail = [UIImage imageWithCGImage:result.thumbnail];videoInfo.videoURL = result.defaultRepresentation.url;NSInteger duration = [[result valueForProperty:ALAssetPropertyDuration] integerValue];videoInfo.duration = [NSDate durationWithSeconds:duration];videoInfo.name = result.defaultRepresentation.filename;videoInfo.size = result.defaultRepresentation.size; //BytesvideoInfo.format = [result.defaultRepresentation.filename pathExtension];videoInfo.creationDate = [result valueForProperty:ALAssetPropertyDate];PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[videoInfo.videoURL] options:nil];PHAsset *phAsset = fetchResult.firstObject;[[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nilresultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {//视频上传不支持慢视频if (avAsset && [avAsset isKindOfClass:[AVURLAsset class]]) {if(videoInfo.thumbnail && videoInfo.videoURL && duration>7) {[reList addObject:videoInfo];}}}];}}];} else {//没有更多的group时,即可认为已经加载完成。if(flag) {//防止重复通知dispatch_async(dispatch_get_main_queue(), ^{//*******//按创建时间倒序NSSortDescriptor *creationDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO];NSArray *descriptorArray = [NSArray arrayWithObjects:creationDateDesc, nil];NSArray *sortedArray = [reList sortedArrayUsingDescriptors: descriptorArray];//*******//通知返回数据[[NSNotificationCenter defaultCenter] postNotificationName:@"ugc_camera_result_notif" object:sortedArray];});flag = false;}}} failureBlock:^(NSError *error) {NSLog(@"error=%@",error);//增加错误返回处理if(flag) {//防止重复通知dispatch_async(dispatch_get_main_queue(), ^{NSArray *sortedArray = [[NSArray alloc] init];//通知返回数据[[NSNotificationCenter defaultCenter] postNotificationName:@"ugc_camera_result_notif" object:sortedArray];});flag = false;}}];});
}
2、将选取的视频存储到本地,上传视频使用
//Output URL
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths.firstObject;
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"mergeSlowMoVideo-%d.mov",arc4random() % 1000]];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];//Begin slow mo video export
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;[exporter exportAsynchronouslyWithCompletionHandler:^{dispatch_async(dispatch_get_main_queue(), ^{if (exporter.status == AVAssetExportSessionStatusCompleted) {NSURL *URL = exporter.outputURL;NSData *videoData = [NSData dataWithContentsOfURL:URL];// Upload[self uploadSelectedVideo:video data:videoData];}});
}];
参考地址:
https://overflow.buffer.com/2016/02/29/slow-motion-video-ios/