从视频创建缩略图 - 选择预览期间要使用的图像

发布于 2024-09-10 02:58:25 字数 968 浏览 7 评论 0原文

使用选择器从视频制作缩略图非常简单。但是,当我在选择器中按“播放”然后选择视频时,我的缩略图始终是黑色的。我希望它能截取屏幕截图 - 然而这种方法只获取视频的第一张图像 - 并且只有在它还没有被播放的情况下!

如何在视频的任意位置制作缩略图?

这里是我用于缩略图的“正常”代码,其中视频尚未播放:

- (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();

不幸的是,按下“播放”后,创建的缩略图是黑色的(仅显示视频滚动和当前视频的 iPhone 屏幕顶部)显示播放位置),缩略图的其余部分始终为黑色。如前所述,在其他情况下它效果很好。

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风和你 2024-09-17 02:58:25

检查 MPMoviePlayerController doku:链接 。有一个“thumbnailImageAtTime:timeOption:”函数返回 UIImage..希望有帮助

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

从此见与不见 2024-09-17 02:58:25

如果你使用 MPMoviePlayerController 你可以尝试这个

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

If you are using MPMoviePlayerController you could try this

- (void)getThumbnailImage
{
UIImage *thumbnailImage = [player thumbnailImageAtTime:player.currentPlaybackTime timeOption:MPMovieTimeOptionExact];
}
塔塔猫 2024-09-17 02:58:25

如果您使用 MPMoviePlayer,则可以使用 MPMoviePlayerControllerthumbnailImageAtTime:timeOption 方法。

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

或者您可以使用以下方法手动从视频中获取缩略图 -

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文