2013-04-13 215 views
3

我想從保存在Documents /文件夾中的視頻獲取視頻縮略圖,並將其顯示在UITableViewCell中的UIImageView中。這裏是我的函數來獲取縮略圖:試圖獲取視頻縮略圖

- (UIImage*) thumbnailImageForVideo:(NSURL *)sourceURL 
{ 
    AVAsset *asset = [AVAsset assetWithURL:sourceURL]; 
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; 
    NSError *err = NULL; 
    CMTime time = CMTimeMake(1, 1); 
    CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&err]; 
    NSLog(@"err==%@, imageRef==%@", err, imageRef); 
    UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imageRef]; 
    CGImageRelease(imageRef); // CGImageRef won't be released by ARC 
    return thumbnail; 
} 

而且這裏是我使用它:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    LibraryCell *libraryCell = [tableView dequeueReusableCellWithIdentifier:@"LibraryCell" forIndexPath:indexPath]; 

    NSString *videoPath = [NSString stringWithFormat:@"%@/%@", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"], [libraryFiles objectAtIndex:indexPath.item]]; 
    libraryCell.itemImage.image = [self thumbnailImageForVideo:[NSURL URLWithString:videoPath]]; 

    return libraryCell; 
} 

的應用程序不會崩潰,但沒有圖像。這是我在控制檯中看到:

2013-04-13 23:44:46.828 GeekOut[14433:907] err==Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo=0x1f0b4610 {NSUnderlyingError=0x1f0b3220 "The operation couldn’t be completed. (OSStatus error -12935.)", NSLocalizedDescription=unknown error}, imageRef==(null) 
2013-04-13 23:44:46.859 GeekOut[14433:907] err==Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo=0x1dd8c660 {NSUnderlyingError=0x1dd8c5c0 "The operation couldn’t be completed. (OSStatus error -12935.)", NSLocalizedDescription=unknown error}, imageRef==(null) 
2013-04-13 23:44:46.893 GeekOut[14433:907] err==Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo=0x1f0bec80 {NSUnderlyingError=0x1f0be9b0 "The operation couldn’t be completed. (OSStatus error -12935.)", NSLocalizedDescription=unknown error}, imageRef==(null) 

回答

13

你的縮略圖提取代碼工作正常。問題是你正在初始化NSURL的方式tableView:cellForRowAtIndexPath:

嘗試修改此:

[NSURL URLWithString:videoPath] 

要這樣:

[NSURL fileURLWithPath:videoPath]