只需调用此方法即可创建视频缩略图。
//实用程序.m
+(void)thumbnailImageForVideoURL:(NSString *)videoURL completionHandler:(void (^)(UIImage *))completionHandler{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *url = [NSURL URLWithString:videoURL];
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
CMTime thumbTime = CMTimeMakeWithSeconds(30,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
if (result != AVAssetImageGeneratorSucceeded) {
NSLog(@"couldn't generate thumbnail, error:%@", error);
}
UIImage *image;
if(im){
image = [UIImage imageWithCGImage:im];
}else{
image = [UIImage imageNamed:@"videoPlaceholder"];
}
completionHandler(image);
};
CGSize maxSize = CGSizeMake(568, 426);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
});
}
从你的班级打电话。
#import "Utilities.h"
[Utilities thumbnailImageForVideoURL:moviePath completionHandler:^(UIImage *thumbImage) {
dispatch_async(dispatch_get_main_queue(), ^{
self.imgViewContent.image = thumbImage;
//self.objectDetail.videoThumbImage = UIImageJPEGRepresentation(thumbImage, 1.0);
});
}];