从NSDocumentDirectory中保存的视频加载缩略图图像

时间:2012-09-09 19:28:34

标签: iphone ios4 avfoundation ios5 nsdocumentdirectory

我使用以下代码从给定路径的视频中获取图像。

- (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;

    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *thumbnailImageGenerationError = nil;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];

    if (!thumbnailImageRef)
        NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError);

    UIImage *thumbnailImage = thumbnailImageRef ? [[UIImage alloc] initWithCGImage:thumbnailImageRef]  : nil;
    previewImage = thumbnailImage;

    return thumbnailImage;
}

但是,这不适用于保存在NSDocumentDirectory中的视频。我们不能使用NSURL访问NSDocumentDIrectory吗?如果没有其他选择。主要思想是显示保存在文档目录中的图像的缩略图,然后允许它上传到服务器。这也可以是临时目录。

1 个答案:

答案 0 :(得分:0)

使用NSURL时工作* outputURL = [[NSURL alloc] initFileURLWithPath:path];