2

我正在尝试在按钮中显示电影缩略图,供用户在上传之前查看。我正在使用 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

4

2 回答 2

1

确保在生成 URL 时使用 [NSURL fileURLWithPath:@"yourpath"] 而不是 [NSURL URLWithString:]

于 2013-04-17T20:05:15.500 回答
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);
             }];
    }
于 2017-02-21T15:02:12.123 回答