从iPhone SDK中的视频网址或数据中获取缩略图

问题描述:

我正在尝试从iphone 3GS相机拍摄的视频中获取(第一帧的)缩略图,以便我可以显示它。有人成功地这样做了吗?如果是这样,怎么做?

I am trying to acquire a thumbnail (of the first frame) from a video taken from iphone 3GS camera so i can display it. Anyone been successful at doing this? If so, how to do?

这个问题的答案是,现在可以使用4.0 iOS获取AVFoundation的缩略图,下面的代码,其中类属性url是电影网址,可以做到这一点(你可以随时获取缩略图,在示例中它的时间为0)

The answer to this question is that one can now with 4.0 iOS get thumbnails using AVFoundation, the following code where the class property url is the movie url, will do the trick (you can get the thumbnail at any time, in the example its at time 0)

-(void)generateImage
{
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    [asset release];
    CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
        if (result != AVAssetImageGeneratorSucceeded) {
            NSLog(@"couldn't generate thumbnail, error:%@", error);
        }
        [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
        thumbImg=[[UIImage imageWithCGImage:im] retain];
        [generator release];
    };

    CGSize maxSize = CGSizeMake(320, 180);
    generator.maximumSize = maxSize;
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];

}