Madhu
Madhu

Reputation: 994

AVAssetImageGenerator not generating Image from Video url

I am using AVAssetImageGenerator for generating the thumbnail images for video play buttons. When i pass server video url to generate image it is retuning null.

    AVAsset *asset = [AVAsset assetWithURL:url];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    CMTime time = CMTimeMake(51,1);
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    UIImage *thumbnail =[[UIImage alloc]initWithCGImage:imageRef];
    CGImageRef cgref = [thumbnail CGImage];
    CIImage *cim = [thumbnail CIImage];

    if (cim == nil && cgref == NULL)
    {
        NSLog(@"no underlying data");
    }
     CGImageRelease(imageRef); 

Always i am getting null data.rarely i am getting only one image. How can i solve this problem.

Upvotes: 3

Views: 4000

Answers (1)

Vishnu
Vishnu

Reputation: 2243

You can try the following code. Its working for me

AVURLAsset *as = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *ima = [[AVAssetImageGenerator alloc] initWithAsset:as];
NSError *err = NULL;
CMTime time = CMTimeMake(0, 60);
CGImageRef imgRef = [ima copyCGImageAtTime:time actualTime:NULL error:&err];
[ima release];
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];

And i got a reference is that the easiest way is to just set the appliesPreferredTrackTransform property on the image generator to YES, then it should automatically do the transformation for you.

Hope this helps !!!

Upvotes: 3

Related Questions