【发布时间】:2014-11-09 02:03:32
【问题描述】:
我知道这个问题在 stackoverflow 上已经被问过很多次了。但我的问题是不同的。
我正在迭代照片库的相册以获取所有视频及其缩略图。
现在,问题是,我的代码获取每个视频的缩略图非常慢。例如,我的相机胶卷中有 14 个视频,生成缩略图的总时间约为 3-4 秒。我正在使用此代码。
<pre></pre>
+(UIImage*)imageFromVideoAtURL:(NSURL*)contentURL {
UIImage* theImage = nil;
AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
AVAssetImageGenerator* generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform = YES;
NSError* err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
theImage = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];
CGImageRelease(imgRef);
[asset release];
[generator release];
return theImage;
}
我正在寻找一种方法来非常快速地获取所有视频的缩略图,这样用户就不必等待了。我在 Apple Store 上看到过在几微秒内完成相同操作的应用程序。请帮忙。
我也试过这段代码生成缩略图,也很慢。
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [[moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame] retain];
[imageView setImage:thumbnail]; //imageView is a UIImageView
【问题讨论】:
-
当您使用图片库中的视频时,您是否尝试过使用
ALAssetRepresentation类的thumbnail方法? -
我猜
ALAssetRepresentation中没有方法thumbnail。是的,我正在使用图片库中的视频。 -
非常抱歉我错误地引用了班级名称。它应该是
ALAsset。它包含thumbnail以及aspectRatioThumbnail方法。 -
谢谢@AyanSengupta,我在“ALAsset”类中找到了这个。会试试的。您能否将其添加为答案,以便我接受。
标签: ios video thumbnails