我正在使用AVAssetImageGenerator
从视频的最后一帧创建图像。这通常可以正常工作,但时不时会copyCGImageAtTime
因错误而失败
NSLocalizedDescription = "Cannot Open";
NSLocalizedFailureReason = "This media cannot be used.";
NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-12431";
我正在验证 AVAsset 不是 nil 并且我直接从资产中提取 CMTime,所以我不明白为什么这种情况会一直发生。这仅在尝试获取最后一帧时发生,如果我改用kCMTimeZero
它,它似乎可以工作。
- (void)getLastFrameFromAsset:(AVAsset *)asset completionHandler:(void (^)(UIImage *image))completion
{
NSAssert(asset, @"Tried to generate last frame from nil asset");
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.requestedTimeToleranceBefore = kCMTimeZero;
gen.requestedTimeToleranceAfter = kCMTimeZero;
gen.appliesPreferredTrackTransform = YES;
CMTime time = [asset duration];
NSError *error = nil;
CMTime actualTime;
CGImageRef imageRef = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
NSAssert(image, @"Failed at generating image from asset's last frame");
completion(image);
CGImageRelease(imageRef);
}
这似乎是相关的,但它并没有解决我的问题。