2013-10-14 21 views
5

ios7私は参考のためにこれを使用しています:Getting thumbnail from a video url or data in IPhone SDK生成サムネイル -

方法ではなくAVFoundationのMPMoviePlayerControllerクラスを使用している、と私はそれにも利用したいと考える人々が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 Iは、(参照としてthis questionを使用して)、サムネイル画像要求の通知を作成する方法を考え出しました。しかし、このメソッドはメインスレッドから非同期で動作するので、通知ハンドラメソッドは呼び出されないようです。

これは私が今持っているものです。

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]; 

そして、私のハンドラ・メソッド:

-(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]; 
} 

しかし、ハンドラが呼び出されることは決してありません(またはそれが表示されます)。

答えて

1

URLがHTTPライブストリームの場合は、ドキュメントごとに何も返されません。ファイルのURLについては、映画の再生後にリクエストを開始しなければならないことが判明しました。

14

この方法を試してください。 * .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

で* .hの

#import <AVFoundation/AVFoundation.h> 

import AVFoundation framework 

は、ビデオのサムネイルを作成し、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]; 
関連する問題