简体   繁体   中英

Unable to generate thumbnail from video url

I am using the this code for generating thumbnail from a video url

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:videoUrl options:nil];
AVAssetImageGenerator *generator = 
[[AVAssetImageGenerator alloc] initWithAsset:asset];
        generator.appliesPreferredTrackTransform=TRUE;
        CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

        AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef imgRef, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
            if (result != AVAssetImageGeneratorSucceeded) {
                NSLog(@"Couldn't generate Thumbnail, error:%@", error);
            }
            UIImage *thumbnailImage = [UIImage imageWithCGImage:imgRef];

            };

        [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];

but i am getting this error.

Couldn't generate Thumbnail, error:Error Domain=AVFoundationErrorDomain 
Code=-11800 "The operation could not be completed" UserInfo=
{NSUnderlyingError=0x174251c40 {Error Domain=NSOSStatusErrorDomain Code=-308 
"(null)"}, NSLocalizedFailureReason=An unknown error occurred (-308), 
NSLocalizedDescription=The operation could not be completed}

Am i doing this right? Could someone please help me out? Thanks

import AVFoundation framework 

Then, import in .h as below

#import <AVFoundation/AVFoundation.h>

and atlast write below code while you want to generate thumbnail from video

AVURLAsset *assetObj = [[AVURLAsset alloc] initWithURL:self.urlForConevW options:nil];
AVAssetImageGenerator *ImgObj = [[AVAssetImageGenerator alloc] initWithAsset:assetObj];
NSError *error = NULL;
CMTime time = CMTimeMake(1, 65);
CGImageRef refImg = [ImgObj copyCGImageAtTime:time actualTime:NULL error:&error];
NSLog(@"error==%@, Refimage==%@", error, refImg);

UIImage *finalImage= [[UIImage alloc] initWithCGImage:refImg];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM