无法从视频网址生成缩略图

时间:2016-10-07 06:52:19

标签: ios iphone xcode avurlasset avassetimagegenerator

我正在使用此代码从视频网址生成缩略图

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];

但是我收到了这个错误。

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}

我这样做对吗?有人可以帮帮我吗?谢谢

1 个答案:

答案 0 :(得分:2)

import AVFoundation framework 

然后,导入.h如下

#import <AVFoundation/AVFoundation.h>

并且当您想要从视频

生成缩略图时,请在下面写下代码
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];