3

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.

Madhu
  • 994
  • 1
  • 12
  • 33
  • Have you resolved this? I am in the same situation where I don't get the image if it's a remote URL – Vig Mar 31 '15 at 12:44

1 Answers1

3

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 !!!

Vishnu
  • 2,243
  • 2
  • 21
  • 44
  • Please check this : https://stackoverflow.com/questions/47617130/error-getting-same-video-thumbnail-image-every-time-in-swift3-ios – Anand Gautam Dec 03 '17 at 16:01