从视频网站异步获取缩略图 - iOS

时间:2015-09-03 10:39:51

标签: ios objective-c iphone asynchronous uiimageview

有许多示例代码可用于从视频网址获取缩略图。但我尝试的所有这些都不是异步生成图像。因此,当涉及到tableview中的多个视频时,应用会冻结。请帮我异步制作这个缩略图。

-(UIImage *)generateThumbnailIconForVideoFileWith:(NSURL *)contentURL WithSize:(CGSize)size
{
    UIImage *theImage = nil;
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.maximumSize=size;
    generator.appliesPreferredTrackTransform = YES;
    NSError *err = NULL;
    CMTime time = CMTimeMake(100,100); //change whatever you want here.
    CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
   theImage = [[UIImage alloc] initWithCGImage:imgRef] ;
   CGImageRelease(imgRef);
   return theImage;

}

1 个答案:

答案 0 :(得分:0)

您还可以使用MPMovieplayer生成缩略图

   MPMoviePlayerController *player= [[MPMoviePlayerController alloc]initWithContentURL:Videourl];
   self.imgshow.image = [player thumbnailImageAtTime:5.0 timeOption:MPMovieTimeOptionNearestKeyFrame];

  [player stop];