i am using AVAssetImageGenerator for generating the thumbnail images for video. when i pass local video asset url to generate image it is retuning nil.i am using below code to generate image.
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);
use below code
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);
Simply call this method to create Video Thumbnail.
//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];
});
}
Call from your class.
#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);
});
}];
Try this code
-(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;
}