我正在尝试在按钮中显示电影缩略图,供用户在上传之前查看。我正在使用AVAssetImageGenerator。代码如下:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:[NSURL URLWithString:moviePath] options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
switch (result) {
case AVAssetImageGeneratorSucceeded:
NSLog(@"Image generator succeeded!");
[imageButton setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
break;
case AVAssetImageGeneratorCancelled:
NSLog(@"Image generator cancelled!");
NSLog(@"couldn't generate thumbnail, error:%@", error);
[imageButton setTitle:@"Video attached" forState:UIControlStateNormal];
break;
case AVAssetImageGeneratorFailed:
NSLog(@"Image generator failed!");
NSLog(@"couldn't generate thumbnail, error:%@", error);
[imageButton setTitle:@"Video attached" forState:UIControlStateNormal];
break;
default:
break;
}
};
CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
上述代码因此错误而失败:
couldn't generate thumbnail, error:Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo=0x1f90a080 {NSUnderlyingError=0x1f979aa0 "The operation couldn’t be completed. (OSStatus error -12935.)", NSLocalizedDescription=unknown error}
来自imagePickeController的moviePath变量是: /private/var/mobile/Applications/89FD1D5F-B49C-4E6B-B1AA-7F1CF1F3FE83/tmp//trim.eaEKqI.MOV
答案 0 :(得分:0)
确保在生成URL时使用[NSURL fileURLWithPath:@“yourpath”]而不是[NSURL URLWithString:]
答案 1 :(得分:0)
这是一个旧的链接,但如果有人仍在寻找答案,请注意重要的是您应该只在资产变为可播放后生成图像。
以下是一些可能涉及这个概念的代码:
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:@{
AVURLAssetReferenceRestrictionsKey: @(AVAssetReferenceRestrictionForbidNone),
AVURLAssetHTTPCookiesKey: cookies,
AVURLAssetAllowsCellularAccessKey: @false
}];
NSArray *requestedKeys = [NSArray arrayWithObjects:
@"playable", nil];
[asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler: ^{
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
[imageGenerator setRequestedTimeToleranceBefore:kCMTimePositiveInfinity];
[imageGenerator setRequestedTimeToleranceAfter:kCMTimePositiveInfinity];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime thumbTime = CMTimeMakeWithSeconds(0,1);
NSError *error = NULL;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:thumbTime actualTime:NULL error:&error];
UIImage *img = [UIImage imageWithCGImage:imageRef];
if (img) {
NSLog(@"Loaded image from %@", [url absoluteString]);
}
else {
NSLog(@"Could not load image from %@, error:%@", [url absoluteString], error);
}
//have to manually release because ARC does not release imageref
CGImageRelease(imageRef);
}];
}