ThumbnailImageAtTime: now not recommended - which alternative?

Prior to the iOS7 update, I used ...

UIImage *image = [moviePlayer thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 

... with great success, so that my application can still show the video that the user has just made.

I understand this method since iOS7 is now deprecated and I need an alternative. I see a method there

 - (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option 

but how can I return an image from it so that I can put it in the image of the videoReview button?

Thanks, Jim.

**** Edited question by trying the notification center method ***

I used the following code -

 [moviePlayer requestThumbnailImagesAtTimes:times timeOption:MPMovieTimeOptionNearestKeyFrame]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerThumbnailImageRequestDidFinishNotification::) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:moviePlayer]; 

I made NSArray times from two NSNumber objects 1 and 2.

Then I tried to capture the notification using the following method

 -(void)MPMoviePlayerThumbnailImageRequestDidFinishNotification: (NSDictionary*)info{ UIImage *image = [info objectForKey:MPMoviePlayerThumbnailImageKey]; 

Then he proceeded to use this thumbnail as a button image as a preview .... but that did not work.

If you see from my encoding where I made a mistake, your help will be appreciated again. Greetings

+22
deprecated image ios7 video nsnotificationcenter
Sep 30 '13 at 23:48
source share
7 answers

I managed to find a great way using AVAssetImageGenerator, see the code below ...

 AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:partOneUrl options:nil]; AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1]; generate1.appliesPreferredTrackTransform = YES; NSError *err = NULL; CMTime time = CMTimeMake(1, 2); CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err]; UIImage *one = [[UIImage alloc] initWithCGImage:oneRef]; [_firstImage setImage:one]; _firstImage.contentMode = UIViewContentModeScaleAspectFit; 

Inside the header file, please import

 #import <AVFoundation/AVFoundation.h> 

It works fine, and I was able to call it from viewDidLoad, which was faster than calling the deprecated thumbNailImageAtTime: from viewDidAppear.

Hope this helps anyone who has the same problem.

+41
Oct 01 '13 at
source share
β€” -

RequestThumbnailImagesAtTimes: method timeOption: will send a notification to MPMoviePlayerThumbnailImageRequestDidFinishNotification when the image request completes. Your code that needs a thumbnail should subscribe to this notification using NSNotificationCenter and use the image when it receives the notification.

+5
01 Oct '13 at 0:11
source share

The way to do this, at least in iOS7, is to use floats for your time

 NSNumber *timeStamp = @1.f; [moviePlayer requestThumbnailImagesAtTimes:timeStamp timeOption:MPMovieTimeOptionNearestKeyFrame]; 

Hope this helps

+2
Apr 02 '14 at 5:59
source share

Jeely provides a good job, but it requires an additional library, which is not needed when MPMoviePlayer already provides functions for this task. I noticed a syntax error in the poster source code. The thumbnail notification handler expects an object of type NSNotification, not a dictionary object. Here is a corrected example:

 -(void)MPMoviePlayerThumbnailImageRequestDidFinishNotification: (NSNotification*)note { NSDictionary * userInfo = [note userInfo]; UIImage *image = (UIImage *)[userInfo objectForKey:MPMoviePlayerThumbnailImageKey]; if(image!=NULL) [thumbView setImage:image]; } 
+2
Apr 19 '14 at 4:29
source share

The problem is that you have to specify float values ​​in requestThumbnailImagesAtTimes .

For example, this will work

 [self.moviePlayer requestThumbnailImagesAtTimes:@[@14.f] timeOption:MPMovieTimeOptionNearestKeyFrame]; 

but this one will not work :

 [self.moviePlayer requestThumbnailImagesAtTimes:@[@14] timeOption:MPMovieTimeOptionNearestKeyFrame]; 
+2
May 27 '14 at 11:35
source share

I just looked for a solution to this problem and got good help from your question. Got your code above to work with one small change, removed the colon ...

Edit

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerThumbnailImageRequestDidFinishNotification::) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:moviePlayer]; 

to

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerThumbnailImageRequestDidFinishNotification:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:moviePlayer]; 

To make it work well. In addition, I found that you cannot call a method that relies on NotificationCenter if you are already in the notification selector. At first I tried - I tried calling requestThumbnailImagesAtTimes inside the notification selector for MPMoviePlayerPlaybackDidFinishNotification - that will not work. I think because the notification will not work.

0
Feb 28 '14 at 0:01
source share

The code in Swift 2.1 will look like this:

 do{ let asset1 = AVURLAsset(URL: url) let generate1: AVAssetImageGenerator = AVAssetImageGenerator(asset: asset1) generate1.appliesPreferredTrackTransform = true let time: CMTime = CMTimeMake(3, 1) //TO CATCH THE THIRD SECOND OF THE VIDEO let oneRef: CGImageRef = try generate1.copyCGImageAtTime(time, actualTime: nil) let resultImage = UIImage(CGImage: oneRef) } catch let error as NSError{ print(error) } 
0
Mar 22 '16 at 16:06
source share



All Articles