Getting video snapshot for thumbnail

random picture random · Feb 5, 2012 · Viewed 14.2k times · Source

I am recording a video from the iPhone camera by using the AVCam code provided from apple.

After the video is recorded it is saved to the photos library.

A new view is then loaded, here I need to have an image thumbnail of the video.

I have a path to the video:

file://localhost/private/var/mobile/Applications/ED45DEFC-ABF9-4A5E-9102-21680CC1448E/tmp/output.mov

I can't seem to figure how to get the first frame of the video to use as a thumbnail.

Any help would be very appreciated and thank you for your time.


EDIT

I ended up using this, I'm not sure why it returns the image sideways?

- (UIImage*)loadImage {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL 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];
    NSLog(@"err==%@, imageRef==%@", err, imgRef);

    return [[UIImage alloc] initWithCGImage:imgRef];

}

Answer

djromero picture djromero · Feb 5, 2012

To fix the thumbnail orientation set appliesPreferredTrackTransform to YES in the AVAssetImageGenerator instance. If you add your own video composition, you'll need to include the right transform to rotate the video as wanted.

generate.appliesPreferredTrackTransform = YES;

Remember to release the obtained image reference with CGImageRelease.

To request multiple thumbnails it's better to do asynchronously with generateCGImagesAsynchronouslyForTimes:completionHandler:.