Reputation: 11053
making a thumbnail from video using the picker is straight forward. However, when I press PLAY in the picker and then chose the video, my thumbnail is alway black. I was hoping it makes a screenshot - however this method only takes the first image of the video - and ONLY IF IT HASN'T BEEN PLAYED!
How can I make a thumbnail at any position of the video?
Here the "normal" code I use for thumbnails, where the video hasn't been played:
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {
CGSize size = viewImage.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = 80.0 / size.width;
} else {
ratio = 80.0 / size.height;
}
CGRect rectForthumbnail = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rectForthumbnail.size);
CGRect clipRect = CGRectMake(0.0, 0.0,74,74);
[viewImage drawInRect:clipRect];
dance.thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
After having pressed "PLAY", unfortunately, the created thumbnail is black (only shows the top of the iphone screen where the video roll and the current play position is displayed), the remaining of the thumbnail is always black. As said, in other cases it works well.
Thanks very much!
Upvotes: 2
Views: 3902
Reputation: 931
You can use MPMoviePlayerController thumbnailImageAtTime:timeOption
method if you are using MPMoviePlayer.
-(UIImage *)thumbnailImageFromVideo:(NSString *)videoPath
{
NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnailImage = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//Player autoplays audio on init
[player stop];
return thumbnailImage;
}
or you can manually get thumbnail image from video using following method -
-(UIImage *)thumbnailCreationForVideo:(NSString *)videoPath
{
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:videoPath];
AVURLAsset *myAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:myAsset];
NSError *err = NULL;
Float64 durationSeconds = CMTimeGetSeconds([myAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
CGImageRef imgRef = [generate copyCGImageAtTime:midpoint actualTime:NULL error:&err];
UIImage *thumbnailImage = [[UIImage alloc] initWithCGImage:imgRef];
return thumbnailImage;
}
Upvotes: 1
Reputation: 26400
If you are using MPMoviePlayerController you could try this
- (void)getThumbnailImage
{
UIImage *thumbnailImage = [player thumbnailImageAtTime:player.currentPlaybackTime timeOption:MPMovieTimeOptionExact];
}
Upvotes: 1