thumbnailImageAtTime: now deprecated - What's the alternative?

后端 未结 7 1259
小鲜肉
小鲜肉 2020-12-04 20:27

Until iOS7 update I was using...

UIImage *image = [moviePlayer thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNea         


        
相关标签:
7条回答
  • 2020-12-04 20:29

    Jeely provides a good work around but it requires an additional library that isn't necessary when the MPMoviePlayer already provides functions for this task. I noticed a syntax error in the original poster's code. The thumbnail notification handler expects an object of type NSNotification, not a dictionary object. Here's a corrected example:

    -(void)MPMoviePlayerThumbnailImageRequestDidFinishNotification: (NSNotification*)note
    {
        NSDictionary * userInfo = [note userInfo];
        UIImage *image = (UIImage *)[userInfo objectForKey:MPMoviePlayerThumbnailImageKey];
        if(image!=NULL)
            [thumbView setImage:image];
    }
    
    0 讨论(0)
  • 2020-12-04 20:31

    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 won't work:

    [self.moviePlayer requestThumbnailImagesAtTimes:@[@14] timeOption:MPMovieTimeOptionNearestKeyFrame];
    
    0 讨论(0)
  • 2020-12-04 20:32

    The code in Swift 2.1 would 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 讨论(0)
  • 2020-12-04 20:39

    The way to do it, at least in iOS7 is to use floats for your times

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

    Hope this helps

    0 讨论(0)
  • 2020-12-04 20:42

    Managed to find a great way using AVAssetImageGenerator, please see 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;
    

    Within header file, please import

    #import <AVFoundation/AVFoundation.h>
    

    It works perfect and I've been able to call it from viewDidLoad, which was quicker than calling the deprecated thumbNailImageAtTime: from the viewDidAppear.

    Hope this helps anyone else who had the same problem.

    * **Update for Swift 5.1 ****

    Useful function...

        func createThumbnailOfVideoUrl(url: URL) -> UIImage? {
            let asset = AVAsset(url: url)
            let assetImgGenerate = AVAssetImageGenerator(asset: asset)
            assetImgGenerate.appliesPreferredTrackTransform = true
    
            let time = CMTimeMakeWithSeconds(1.0, preferredTimescale: 600)
            do {
                let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) 
                let thumbnail = UIImage(cgImage: img)
                return thumbnail
            } catch {
              print(error.localizedDescription)
              return nil
            }
        }
    
    0 讨论(0)
  • 2020-12-04 20:49

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

    0 讨论(0)
提交回复
热议问题