Iphone AVURLAsset和AVAssetImageGenerator返回空图像

Iphone AVURLAsset和AVAssetImageGenerator返回空图像,iphone,thumbnails,avfoundation,Iphone,Thumbnails,Avfoundation,我正在尝试从iphone摄像头捕获的.mov文件中获取缩略图。我目前已将电影保存在应用程序的文档部分。当我调用[Asset duration]时,它返回一个空对象。另外,当我尝试调用copyCGImageAtTime:actualtime:error方法时,它也会返回一个null对象。我花了不到几个小时的时间试图弄明白这一点。我试着将代码移动到我应用程序的另一个主要部分,看看是否可以让它工作。我还尝试在模拟器上运行它,但没有成功。代码如下: NSString* destinationPath =

我正在尝试从iphone摄像头捕获的.mov文件中获取缩略图。我目前已将电影保存在应用程序的文档部分。当我调用[Asset duration]时,它返回一个空对象。另外,当我尝试调用copyCGImageAtTime:actualtime:error方法时,它也会返回一个null对象。我花了不到几个小时的时间试图弄明白这一点。我试着将代码移动到我应用程序的另一个主要部分,看看是否可以让它工作。我还尝试在模拟器上运行它,但没有成功。代码如下:

NSString* destinationPath = [NSString stringWithFormat:@"%@/aaa/aaa.mov", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:destinationPath] options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error2 = nil;
CMTime actualTime;

CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error2];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);   
我还确认了该文件夹下确实存在该电影。任何帮助都将不胜感激。谢谢:)

--编辑--

忘记提到copyCGImageAtTime的错误是AVUnknown错误

--编辑2--
发现了问题。我没有在url的开头包含file://文件。它现在可以工作了。

发现了问题。我没有在url的开头包含
文件://
。现在可以使用了。

您应该使用[NSURL fileURLWithPath:destinationPath]而不是URLWithString!!indiekiduk给出了最佳解决方案;)您发现了问题,但您的代码有点难看,请改用:[NSURL fileURLWithPath:path]