从iPhone SDK中的videourl或数据获取缩略图

我试图从iPhone 3GS摄像头拍摄的video中获取缩略图(第一帧),以便我可以显示它。 任何人都能成功做到这一点? 如果是的话,该怎么办?

这个问题的答案是,现在可以使用4.0 iOS获取使用AVFoundation的缩略图,下面的代码(其中的类属性url是电影的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]; } 
 -(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 
 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]; 

检查这个链接的替代scheme: thumbnailImageAtTime:现在已弃用 – 有什么替代?

我find的最佳方法… MPMoviePlayerController thumbnailImageAtTime:timeOption

SWIFT 2.0

你可以用两种方法快速生成1. AVFoundation 2. MPMoviePlayerController

 1. func generateThumnail(url : NSURL) -> UIImage{ var asset : AVAsset = AVAsset.assetWithURL(url) as AVAsset var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true var error : NSError? = nil var time : CMTime = CMTimeMake(1, 30) var img : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error) var frameImg : UIImage = UIImage(CGImage: img)! return frameImg } 2. override func viewDidLoad() { super.viewDidLoad() var moviePlayer : MPMoviePlayerController! = MPMoviePlayerController(contentURL: moviePlayManager.movieURL) moviePlayer.view.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: self.view.frame.height) moviePlayer.fullscreen = true moviePlayer.controlStyle = MPMovieControlStyle.None NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoThumbnailIsAvailable:", name: MPMoviePlayerThumbnailImageRequestDidFinishNotification, object: nil) let thumbnailTimes = 3.0 moviePlayer.requestThumbnailImagesAtTimes([thumbnailTimes], timeOption: .NearestKeyFrame) } func videoThumbnailIsAvailable(notification: NSNotification){ if let player = moviePlayer{ let thumbnail = notification.userInfo![MPMoviePlayerThumbnailImageKey] as? UIImage if let image = thumbnail{ /* We got the thumbnail image. You can now use it here */ println("Thumbnail image = \(image)") } } 

Swift 2代码:

 func previewImageForLocalVideo(url:NSURL) -> UIImage? { let asset = AVAsset(URL: url) let imageGenerator = AVAssetImageGenerator(asset: asset) imageGenerator.appliesPreferredTrackTransform = true var time = asset.duration //If possible - take not the first frame (it could be completely black or white on camara's videos) time.value = min(time.value, 2) do { let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil) return UIImage(CGImage: imageRef) } catch let error as NSError { print("Image generation failed with error \(error)") return nil } } 

对于Swift 3.0

 func createThumbnailOfVideoFromFileURL(_ strVideoURL: String) -> UIImage?{ let asset = AVAsset(url: URL(string: strVideoURL)!) let assetImgGenerate = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true let time = CMTimeMakeWithSeconds(Float64(1), 100) do { let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) let thumbnail = UIImage(cgImage: img) return thumbnail } catch { /* error handling here */ } return nil } 

也许这对面临同样问题的其他人有用。 我需要一个简单的解决scheme来创build图片,PDF和video的缩略图。 为了解决这个问题,我创build了下面的库。

https://github.com/prine/ROThumbnailGenerator

用法非常简单: var thumbnailImage = ROThumbnail.getThumbnail(url)

它在内部有三个不同的实现,并根据文件扩展名创build缩略图。 如果你需要一个缩略图创build器来创build另一个文件扩展名,你可以轻松地添加你自己的实现。

Swift 2.1以所需的时间间隔获取缩略图

 func getPreviewImageForVideoAtURL(videoURL: NSURL, atInterval: Int) -> UIImage? { print("Taking pic at \(atInterval) second") let asset = AVAsset(URL: videoURL) let assetImgGenerate = AVAssetImageGenerator(asset: asset) assetImgGenerate.appliesPreferredTrackTransform = true let time = CMTimeMakeWithSeconds(Float64(atInterval), 100) do { let img = try assetImgGenerate.copyCGImageAtTime(time, actualTime: nil) let frameImg = UIImage(CGImage: img) return frameImg } catch { /* error handling here */ } return nil } 

当您将“fileURLWithPath”更改为“URLWithString”时,您将从URL获得缩略图。

在我的情况下,它是这样的。

 NSURL *url = [NSURL URLWithString:filepath]; AVAsset *asset = [AVAsset assetWithURL:url]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; CMTime time = [asset duration]; time.value = 0; CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return thumbnail;