从附加到AVPlayer的CALayer获取UIImage(从视频播放中提取帧)
我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法让 Meet 的解决方案为我所用,但它让我朝着正确的方向思考。
下面是我最终在项目中使用的代码。
screenshotFromPlayer:maximumSize:
方法接受一个用于截取屏幕截图的AVPlayer
实例,以及一个作为返回图像最大值的CGSize
尺寸。另请注意,可以使用方法
generateCGImagesAsynchronouslyForTimes:completionHandler:
而不是copyCGImageAtTime:actualTime:error:
(在AVAssetImageGenerator
的实例上)来执行图像异步生成。此代码示例在
AVPlayer
的currentTime
生成屏幕截图,但也可以使用任何时间。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 anAVPlayer
from which to take a screenshot, and aCGSize
that will be the returned image's maximum size.Note also that one could use the method
generateCGImagesAsynchronouslyForTimes:completionHandler:
instead ofcopyCGImageAtTime:actualTime:error:
(on the instance ofAVAssetImageGenerator
) to perform the image generation asynchronously.This code sample generates a screenshot at the
AVPlayer
'scurrentTime
, but any time could be used instead.从 avplayer 获取图像的代码。
如果您需要精确的时间,请设置
[imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero]
和[imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero]
。The code to get image from avplayer.
set
[imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero]
and[imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero]
if you need precise time.尝试使用 AVAssetImageGenerator 从指定实例的视频文件中获取图像:
Try to get the image from the video file at specified instance using the AVAssetImageGenerator: