Creating Thumbnail for Video in iOS

前端 未结 5 1222
别跟我提以往
别跟我提以往 2020-12-05 03:12

I have an application that I am developing for the iPhone. What it does is, it captures the video from the camera and stores the video file onto the File System.

I

5条回答
  •  鱼传尺愫
    2020-12-05 03:46

    Try this (it doesn't actually show the movie player):

    + (UIImage *)imageFromMovie:(NSURL *)movieURL atTime:(NSTimeInterval)time {
      // set up the movie player
      MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] 
        initWithContentURL:movieURL];
      mp.shouldAutoplay = NO;
      mp.initialPlaybackTime = time;
      mp.currentPlaybackTime = time;
      // get the thumbnail
      UIImage *thumbnail = [mp thumbnailImageAtTime:time 
                               timeOption:MPMovieTimeOptionNearestKeyFrame];
      // clean up the movie player
      [mp stop];
      [mp release];
      return(thumbnail);
    }
    

    It's supposed to be a synchronous call, so it might block the main thread some, but seems to be running pretty instantly for me when I use a time at the beginning of the movie. If you're doing this a lot, you can add it as a category on UIImage, which is what I did.

    I see from your question that you want to do this before the movie is saved, and I guess it might not work without a file url. However, if you're using the UIImagePickerController for camera capture, you can pass this function the URL returned in the info dictionary of imagePickerController:didFinishPickingMediaWithInfo: with the key UIImagePickerControllerMediaURL.

提交回复
热议问题