AVURLAsset和AVAssetImageGenerator返回空图像

3

我正在尝试从我从iPhone相机捕获的.mov文件中获取缩略图。我目前将电影保存在应用程序的“文档”部分。当我调用 [Asset duration] 时,它返回一个空对象。当我尝试调用copyCGImageAtTime:actualtime:error方法时,它也返回一个空对象。我已经花费了无数个小时来解决这个问题。我尝试将我的代码移动到应用程序的另一个主要部分,只是为了看看是否能让它工作。我还尝试在模拟器上运行它,但没有成功。下面是代码:

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://。现在它可以工作了。

2
你应该使用[NSURL fileURLWithPath:destinationPath]而不是URLWithString!! - malhal
indiekiduk 给出了最好的解决方案 ;) - Climbatize
你找到了问题,但是你的代码有点丑陋, 使用这个替代:[NSURL fileURLWithPath:path] - João Nunes
1个回答

9

发现问题所在,我没有在URL的开头加上file://。现在它可以正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接