开发者

Creating thumbnail from video - choosing the image to be used during preview

开发者 https://www.devze.com 2023-01-07 21:48 出处:网络
making a thumbnail from video using the picker is stra开发者_开发问答ight forward. However, when I press PLAY in the picker and then chose the video, my thumbnail is alway black. I was hoping it makes

making a thumbnail from video using the picker is stra开发者_开发问答ight 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!


Check the MPMoviePlayerController doku: link. There´s a "thumbnailImageAtTime:timeOption:" function which returns a UIImage.. Hope that helps


If you are using MPMoviePlayerController you could try this

- (void)getThumbnailImage
{
UIImage *thumbnailImage = [player thumbnailImageAtTime:player.currentPlaybackTime timeOption:MPMovieTimeOptionExact];
}


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消