Thumbnail image of video

DipakSonara picture DipakSonara · Jan 18, 2012 · Viewed 22.9k times · Source

I am selecting video clip from library. And i want to create thumbnail image of it. I have applied this code. But the image appeared rotated. I want its original view.

- (UIImage*)testGenerateThumbNailDataWithVideo {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:appDelegate.videoURL options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
    [generate release];
    NSLog(@"err==%@, imageRef==%@", err, imgRef);
    UIImage *currentImg = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];
    static BOOL flag = YES; 
    if (flag) { 
        NSData *tmpData =   UIImageJPEGRepresentation(currentImg, 0.8);
        NSString *path = [NSString stringWithFormat:@"%@thumbNail.png", NSTemporaryDirectory()];
        BOOL ret = [tmpData writeToFile:path atomically:YES]; 
        NSLog(@"write to path=%@, flag=%d", path, ret);
        flag = NO;
    }
    return currentImg;
}

Answer

Chetan Bhalara picture Chetan Bhalara · Jan 18, 2012

Try using AVAssetImageGenerator instead. Apple discusses using AVAssetImageGenerator to create thumbnails here. Here is sample code, which grabs a single thumbnail image. You will need to include the AVFoundation framework. And also add CoreMedia framework

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidPath options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
[gen release];

One more solution is

-(void)generateImage
{
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    [asset release];
    CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
        if (result != AVAssetImageGeneratorSucceeded) {
            NSLog(@"couldn't generate thumbnail, error:%@", error);
        }
        [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
        thumbImg=[[UIImage imageWithCGImage:im] retain];
        [generator release];
    };

    CGSize maxSize = CGSizeMake(320, 180);
    generator.maximumSize = maxSize;
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];

}

Or

ALAsset
display image from URL retrieved from ALAsset in iPhone