2013-10-14 42 views
5

我使用此以供參考:Getting thumbnail from a video url or data in IPhone SDK生成縮略圖 - ios7

的方法是使用的MPMoviePlayerController類而不是AVFoundation,我想我要使用,以及因爲人說MPMoviePlayer方式比AVFoundation的方式更快。

的問題是,用於創建縮略圖的方法中,[player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]在IOS 7.0已棄用。

通過查看蘋果文檔,其餘支持的創建縮略圖的方法是通過方法(void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option(void)cancelAllThumbnailImageRequests。但是,隨着方法簽名的規定,這些方法什麼都不會返回。那麼如何訪問這些方法創建的UIImage縮略圖?

如果有幫助,這是我到目前爲止在代碼方面:

self.videoURL = info[UIImagePickerControllerMediaURL]; 
    NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL]; 

    //Create thumbnail image 
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL]; 
    [player requestThumbnailImagesAtTimes:@[@1] timeOption:MPMovieTimeOptionNearestKeyFrame]; 
    //UIImage *thumbnail = ??? 

我如何獲得一個UIImage參考縮略圖?

EDIT 我想出如何創建用於縮略圖圖像請求(使用this question作爲參考)的通知。但是,我意識到這種方法與主線程異步工作,所以我的通知處理程序方法似乎永遠不會被調用。

這是我現在有。

-(void)handleThumbnailImageRequestFinishNotification:(NSNotification*)notification 
{ 
    NSDictionary *userinfo = [notification userInfo]; 
    NSError* value = [userinfo objectForKey:MPMoviePlayerThumbnailErrorKey]; 
if (value != nil) 
{ 
    NSLog(@"Error creating video thumbnail image. Details: %@", [value debugDescription]); 
} 
else 
{ 
    UIImage *thumbnail = [userinfo valueForKey:MPMoviePlayerThumbnailImageKey]; 
} 

但處理程序不會被調用(或因此纔出現):

self.videoURL = info[UIImagePickerControllerMediaURL]; 
    NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL]; 
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThumbnailImageRequestFinishNotification:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:player]; 
    [player requestThumbnailImagesAtTimes:@[@1] timeOption:MPMovieTimeOptionNearestKeyFrame]; 

然後我的處理方法。

回答

1

如果你的網址是HTTP實時流,那麼它不會返回任何東西,每個文檔。對於文件URL,我發現我必須在播放電影后開始請求,否則它將永遠不會被調用。

14

試試這個方法。

import AVFoundation framework 

中的* .h

#import <AVFoundation/AVFoundation.h> 

在* .M

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:self.urlForConevW options:nil]; 
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset]; 
NSError *error = NULL; 
CMTime time = CMTimeMake(1, 65); 
CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error]; 
NSLog(@"error==%@, Refimage==%@", error, refImg); 

UIImage *FrameImage= [[UIImage alloc] initWithCGImage:refImg]; 
+1

作品,謝謝。 – c0d3Junk13

+0

@ c0d3Junk13歡迎您:) – sankalp

3

這裏是讓視頻的縮略圖和圖片保存到DocumentDirectory一個代碼..

//pass the video_path to NSURL 
NSURL *videoURL = [NSURL fileURLWithPath:strVideoPath]; 

AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset1]; 
generator.appliesPreferredTrackTransform = YES; 

//Set the time and size of thumbnail for image 
NSError *err = NULL; 
CMTime thumbTime = CMTimeMakeWithSeconds(0,30); 
CGSize maxSize = CGSizeMake(425,355); 
generator.maximumSize = maxSize; 

CGImageRef imgRef = [generator copyCGImageAtTime:thumbTime actualTime:NULL error:&err]; 
UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imgRef]; 

//And you can save the image to the DocumentDirectory 
NSData *data = UIImagePNGRepresentation(thumbnail); 

//Path for the documentDirectory 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
[data writeToFile:[documentsDirectory stringByAppendingPathComponent:currentFileName] atomically:YES];