我正在尝试为视频文件创建缩略图:
- (UIImage*) thumbnailForVideoAtURL: (NSURL*) videoURL
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
CGImageRef imageHandle = [generator copyCGImageAtTime:kCMTimeZero actualTime:NULL error:NULL];
if (imageHandle) {
UIImage *frameImage = [UIImage imageWithCGImage:imageHandle];
CFRelease(imageHandle);
return frameImage;
} else {
return nil;
}
}
问题是视频文件存储在内容可寻址存储中并且没有扩展名。随着资产的创建,这似乎会AVURLAsset
消失,但是在读取帧图像时,我收到以下错误:
Error Domain=AVFoundationErrorDomain Code=-11828 "Cannot Open" UserInfo=0x167170 {NSLocalizedFailureReason=This media format is not supported., NSUnderlyingError=0x163a00 "The operation couldn’t be completed. (OSStatus error -12847.)", NSLocalizedDescription=Cannot Open}
这是在某处记录或提到的吗?我不敢相信我真的被迫通过文件名传递文件格式信息。初始化程序的options
参数AVURLAsset
看起来是提供文件类型的好地方,但根据文档,似乎没有任何支持。
PS。我已经测试了代码,具有正确扩展名的相同文件可以很好地生成缩略图。