我正在使用 AVAssetImageGenerator 来生成视频的缩略图。当我通过本地视频资产网址生成图片时,它正在重新调整 nil 。我正在使用下面的代码生成图片。
NSURL *url = [NSURL fileURLWithPath:filepath];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time =CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
答案 0 :(得分:2)
只需调用此方法即可创建视频缩略图。
// Utilities.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);
});
}];
答案 1 :(得分:0)
试试此代码
-(UIImage *)generateThumbImage : (NSString *)filepath{
NSURL *url = [NSURL fileURLWithPath:filepath];
UIImage *theImage = nil;
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
theImage = [[UIImage alloc] initWithCGImage:imgRef] ;
CGImageRelease(imgRef);
return theImage;
}
答案 2 :(得分:0)
使用下面的代码
AVAsset *asset = [AVAsset assetWithURL:url];
// Get thumbnail at the very start of the video
CMTime thumbnailTime = [asset duration];
thumbnailTime.value = 0;
// Get image from the video at the given time
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbnailTime actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);