some_id
some_id

Reputation: 29896

How to grab a single frame from a video in the app's dir?

How does one use a UIImageView to show a .mov files visible frame, the preview frame that is visible in e.g. finder or any youtube video etc.

I would like to be able to get the preview frame from .mov files saved to an apps library directory, so they will not be AVAssets.

Upvotes: 1

Views: 282

Answers (1)

memmons
memmons

Reputation: 40502

The AVAssetImageGenerator in AVFoundation can be used to load videos both in albums and the local app dirs.

Here's a helper method that will return an image from a video URL (inside or outside the app) at any given time interval:

+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *thumbnailImageGenerationError = nil;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];

    NSAssert(thumbnailImageRef, @"CGImageRef shall never be nil.");

    UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef] : nil;

    return thumbnailImage;
}

Upvotes: 2

Related Questions