Creating Thumbnail for Video in iOS

Abishek picture Abishek · May 11, 2011 · Viewed 18.2k times · Source

I have an application that I am developing for the iPhone. What it does is, it captures the video from the camera and stores the video file onto the File System.

I need to create a Thumbnail Image of this video file before I save the Image to the File System. My motive is to show a list of thumbnails of the created video so that the user can select a specific thumbnail to play the desired file.

Could someone please advise on how I can create a Thumbnail image of a video file that has been captured by the Camera.

Also, can you please advise if I can create a Thumbnail of an existing video file using iOS SDK.

Answer

Werner Altewischer picture Werner Altewischer · May 20, 2012

A better solution actually is to use the AVFoundation framework to do this. It bypasses the need to construct an MPMoviePlayerController which causes the problem that the Iris of the camera remains closed if used in conjuction with the UIImagePickerController (at least that's what I experienced).

The code I use:

+ (UIImage *)thumbnailFromVideoAtURL:(NSURL *)contentURL {
    UIImage *theImage = nil;
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL 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] autorelease];

    CGImageRelease(imgRef);
    [asset release];
    [generator release];

    return theImage;
}