Search code examples
iosobjective-cthumbnailsvideo-thumbnails

Creating Thumbnail for Video in iOS


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 need to create a Thumbnail Image of this video file before I save the Image to the File System. My motive is to show a list of thumbnails of the created video so that the user can select a specific thumbnail to play the desired file.

Could someone please advise on how I can create a Thumbnail image of a video file that has been captured by the Camera.

Also, can you please advise if I can create a Thumbnail of an existing video file using iOS SDK.


Solution

  • 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.