Prateek sharma
Prateek sharma

Reputation: 239

How to get an frame as an Image from the Recorded Video -AVFoundation (Image as a thumbnail)

I want to extract an image from the recorded video . I was using this code m but no results , Image is not showing up.

I am using the code below for doing this task

AVURLAsset* asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:videoPath] options:nil];
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
UIImage* image = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];
[videoFrame setImage:image];

Please help with a code use to extract image from recorded video

Upvotes: 0

Views: 2501

Answers (2)

SAMIR RATHOD
SAMIR RATHOD

Reputation: 3506

Try this

    NSURL *videoURl = [NSURL fileURLWithPath:videoPath];
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
    AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generate.appliesPreferredTrackTransform = YES;
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
     UIImage *img = [[UIImage alloc] initWithCGImage:imgRef];
    [videoFrame setImage:img];
    [img release];

Upvotes: 5

Nishant Tyagi
Nishant Tyagi

Reputation: 9913

Use this method for generating thumbnail:

#pragma mark -
#pragma mark THUMBNAIL GENARATION
- (UIImage*)testGenerateThumbNailDataWithVideo:(NSURL *)videoURL {

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL 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 *currentImg = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);

    return currentImg;
}

Hope it helps you.

Upvotes: 0

Related Questions