从iOS中的视频网址或数据中获取缩略图我试图从iphone 3GS相机拍摄的视频中获取(第一帧的)缩略图,以便我可以显示它。这该怎么做?
3 回答
暮色呼如
TA贡献1853条经验 获得超9个赞
这个问题的答案是,现在可以使用4.0 iOS获取使用AVFoundation的缩略图,下面的代码,其中类属性url是电影网址,将执行操作(您可以随时获取缩略图,在示例中它可以时间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];}
慕斯709654
TA贡献1840条经验 获得超5个赞
-(UIImage *)generateThumbImage : (NSString *)filepath{ NSURL *url = [NSURL fileURLWithPath:filepath]; AVAsset *asset = [AVAsset assetWithURL:url]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; imageGenerator.appliesPreferredTrackTransform = YES; CMTime time = [asset duration]; time.value = 0; CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); // CGImageRef won't be released by ARC return thumbnail;}
您可以使用time.value获取帧,假设您想要1秒帧然后使用
time.value = 1000 //Time in milliseconds
慕标5832272
TA贡献1966条经验 获得超4个赞
NSURL *videoURL = [NSURL fileURLWithPath:url];MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; //Player autoplays audio on init [player stop]; [player release];
- 3 回答
- 0 关注
- 816 浏览
添加回答
举报
0/150
提交
取消