将之前的获取视频帧的方法回顾总结一下:
方式一:获取相关帧集
AVURLAsset *urlSet = [[AVURLAsset alloc]initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:urlSet];
generator.appliesPreferredTrackTransform = YES;
NSLog(@"generator = %@",generator);
CMTime time = CMTimeMake(1, 30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
UIImage *thumbImg = [UIImage imageWithCGImage:image];
if (result == AVAssetImageGeneratorSucceeded) {//成功即显示
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = thumbImg;
cell.tags = 1;
});
}
};
generator.maximumSize = CGSizeMake(200, 200);
[generator generateCGImagesAsynchronouslyForTimes:
[NSArray arrayWithObject:[NSValue valueWithCMTime:time]] completionHandler:handler];
方式二:得到某时刻帧特征图片 容易获取失败
AVAsset *urlSet = [AVAsset assetWithURL:url];
AVAssetImageGenerator *gener = [[AVAssetImageGenerator alloc]initWithAsset:urlSet];
CMTime actualTime;
CGImageRef imageRef = [gener copyCGImageAtTime:CMTimeMakeWithSeconds(1, 10) actualTime:&actualTime error:nil];
CMTimeShow(actualTime);
/////这里的nserror需判断是否为nil,不为nil返回
NSString *key = [_cameraFilesArr[indexPath.row] componentsSeparatedByString:@"."][0];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *thumbImg = [UIImage imageWithCGImage:imageRef];
cell.imageView.image = thumbImg;
[_imgCache setObject:thumbImg forKey:key]; // 缓存
});