从附加到AVPlayer的CALayer获取UIImage(从视频播放中提取帧)

发布于 2024-10-04 08:59:26 字数 515 浏览 3 评论 0原文

我用 AVPlayer 播放视频。工作正常。

现在我想从播放的视频中获取一个 UIImage (当我暂时按下按钮时)。

我的 AVPlayer 上有一个 CALayer,用于在我的 UIView 上显示视频。 我的想法是在视频播放期间从 CALayer 获取 UIImage

我使用另一个问题中的代码来执行此操作:

UIImage from CALayer - iPhone SDK

但是我的 <代码>UIImage 为空。分辨率很好,但它是全白的!

视频似乎没有写入我的 CALayer 的内容。

有人可以帮助我吗? 谢谢

I play a video with AVPlayer. It works ok.

Now I want to get a UIImage from the video playing (when I push a button for the moment).

Attached to my AVPlayer there is a CALayer that is used to display video on my UIView.
My idea is to get an UIImage from CALayer during video is playing.

I do this with code from another question:

UIImage from CALayer - iPhone SDK

However my UIImage is empty. The resolution is good but it is but fully white !!!

It seems that video doesn't write the contents of my CALayer.

Someone can help me?
Thanks

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

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

发布评论

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

评论(3

夜灵血窟げ 2024-10-11 08:59:26

我无法让 Meet 的解决方案为我所用,但它让我朝着正确的方向思考。

下面是我最终在项目中使用的代码。 screenshotFromPlayer:maximumSize: 方法接受一个用于截取屏幕截图的 AVPlayer 实例,以及一个作为返回图像最大值的 CGSize尺寸。

- (UIImage *)screenshotFromPlayer:(AVPlayer *)player maximumSize:(CGSize)maxSize {

    CMTime actualTime;
    NSError *error;

    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:player.currentItem.asset];

    // Setting a maximum size is not necessary for this code to 
    // successfully get a screenshot, but it was useful for my project.
    generator.maximumSize = maxSize;

    CGImageRef cgIm = [generator copyCGImageAtTime:player.currentTime 
                                        actualTime:&actualTime 
                                             error:&error];
    UIImage *image = [UIImage imageWithCGImage:cgIm];
    CFRelease(cgIm);

    if (nil != error) {
        NSLog(@"Error making screenshot: %@", [error localizedDescription]);
        NSLog(@"Actual screenshot time: %f Requested screenshot time: %f", CMTimeGetSeconds(actualTime),
          CMTimeGetSeconds(self.recordPlayer.currentTime));
        return nil;
    }

    return image;
}

另请注意,可以使用方法 generateCGImagesAsynchronouslyForTimes:completionHandler: 而不是 copyCGImageAtTime:actualTime:error: (在 AVAssetImageGenerator 的实例上)来执行图像异步生成。

此代码示例在 AVPlayercurrentTime 生成屏幕截图,但也可以使用任何时间。

I couldn't get Meet's solution to work for me, but it got me thinking in the right direction.

Below is the code that I ended up using in my project. The method screenshotFromPlayer:maximumSize: accepts an instance of an AVPlayer from which to take a screenshot, and a CGSize that will be the returned image's maximum size.

- (UIImage *)screenshotFromPlayer:(AVPlayer *)player maximumSize:(CGSize)maxSize {

    CMTime actualTime;
    NSError *error;

    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:player.currentItem.asset];

    // Setting a maximum size is not necessary for this code to 
    // successfully get a screenshot, but it was useful for my project.
    generator.maximumSize = maxSize;

    CGImageRef cgIm = [generator copyCGImageAtTime:player.currentTime 
                                        actualTime:&actualTime 
                                             error:&error];
    UIImage *image = [UIImage imageWithCGImage:cgIm];
    CFRelease(cgIm);

    if (nil != error) {
        NSLog(@"Error making screenshot: %@", [error localizedDescription]);
        NSLog(@"Actual screenshot time: %f Requested screenshot time: %f", CMTimeGetSeconds(actualTime),
          CMTimeGetSeconds(self.recordPlayer.currentTime));
        return nil;
    }

    return image;
}

Note also that one could use the method generateCGImagesAsynchronouslyForTimes:completionHandler: instead of copyCGImageAtTime:actualTime:error: (on the instance of AVAssetImageGenerator) to perform the image generation asynchronously.

This code sample generates a screenshot at the AVPlayer's currentTime, but any time could be used instead.

简美 2024-10-11 08:59:26

从 avplayer 获取图像的代码。

 - (UIImage *)currentItemScreenShot
{
    AVPlayer *abovePlayer = [abovePlayerView player];
    CMTime time = [[abovePlayer currentItem] currentTime];
    AVAsset *asset = [[[abovePlayerView player] currentItem] asset];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) {
        [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
        [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];
    }
    CGImageRef imgRef = [imageGenerator copyCGImageAtTime:time
                                          actualTime:NULL
                                               error:NULL];
    if (imgRef == nil) {
        if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) {
            [imageGenerator setRequestedTimeToleranceBefore:kCMTimePositiveInfinity];
            [imageGenerator setRequestedTimeToleranceAfter:kCMTimePositiveInfinity];
        }
        imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    }
    UIImage *image = [[UIImage alloc] initWithCGImage:imgRef];
    CGImageRelease(imgRef);
    [imageGenerator release];

    return [image autorelease];
}

如果您需要精确的时间,请设置 [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero][imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero]

The code to get image from avplayer.

 - (UIImage *)currentItemScreenShot
{
    AVPlayer *abovePlayer = [abovePlayerView player];
    CMTime time = [[abovePlayer currentItem] currentTime];
    AVAsset *asset = [[[abovePlayerView player] currentItem] asset];
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) {
        [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
        [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];
    }
    CGImageRef imgRef = [imageGenerator copyCGImageAtTime:time
                                          actualTime:NULL
                                               error:NULL];
    if (imgRef == nil) {
        if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) {
            [imageGenerator setRequestedTimeToleranceBefore:kCMTimePositiveInfinity];
            [imageGenerator setRequestedTimeToleranceAfter:kCMTimePositiveInfinity];
        }
        imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    }
    UIImage *image = [[UIImage alloc] initWithCGImage:imgRef];
    CGImageRelease(imgRef);
    [imageGenerator release];

    return [image autorelease];
}

set [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero] and [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero] if you need precise time.

探春 2024-10-11 08:59:26

尝试使用 AVAssetImageGenerator 从指定实例的视频文件中获取图像:

    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:[info objectForKey:@"UIImagePickerControllerReferenceURL"]] options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    [asset release];
    CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {

    }
    UIImage *thumbImg = [[UIImage imageWithCGImage:im] retain];
    [generator release];

Try to get the image from the video file at specified instance using the AVAssetImageGenerator:

    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:[info objectForKey:@"UIImagePickerControllerReferenceURL"]] options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform=TRUE;
    [asset release];
    CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {

    }
    UIImage *thumbImg = [[UIImage imageWithCGImage:im] retain];
    [generator release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文