ios - 如何从 AVCaptureVideoPreviewLayer 获取 UIImage?

标签 ios uiimage avfoundation opengl-es-2.0 calayer

我已经尝试过这个解决方案 CGImage (or UIImage) from a CALayer

但是我没有得到任何东西。

如问题所述,我正在尝试从相机的预览层获取 UIImage。我知道我可以捕获静止图像或使用 outputsamplebuffer,但我的 session 质量视频设置为照片,因此这 2 个方法中的任何一个都很慢并且会给我一个大图像。

所以我认为可行的方法是直接从预览层获取图像,因为这正是我需要的尺寸并且已经对其进行了操作。我只是不知道如何让这一层绘制到我的上下文中,以便我可以将其作为 UIImage 获取。

也许另一种解决方案是使用 OpenGL 将这一层直接作为纹理获取?

任何帮助将不胜感激,谢谢。

最佳答案

从这个 Technical Q&A 中引用 Apple :

A: Starting from iOS 7, the UIView class provides a method -drawViewHierarchyInRect:afterScreenUpdates:, which lets you render a snapshot of the complete view hierarchy as visible onscreen into a bitmap context. On iOS 6 and earlier, how to capture a view's drawing contents depends on the underlying drawing technique. This new method -drawViewHierarchyInRect:afterScreenUpdates: enables you to capture the contents of the receiver view and its subviews to an image regardless of the drawing techniques (for example UIKit, Quartz, OpenGL ES, SpriteKit, AV Foundation, etc) in which the views are rendered

根据我对 AVFoundation 的经验,如果您在托管预览层的 View 上使用该方法,您将只能获得 View 的内容而没有预览层的图像。使用 -snapshotViewAfterScreenUpdates: 将返回承载特殊层的 UIView。如果您尝试从该 View 制作图像,您将什么也看不到。
我知道的唯一解决方案是 AVCaptureVideoDataOutputAVCaptureStillImageOutput。每一个都有自己的极限。第一个不能与 AVCaptureMovieFileOutput 采集同时工作,后者会产生快门噪音。

关于ios - 如何从 AVCaptureVideoPreviewLayer 获取 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10977168/

相关文章:

ios - 如何快速将 View Controller 放入 ScrollView 中

ios - 如何更好地初始化图像: with initWithData method or with imageWithData?

iphone - 使用 AVFoundation 保存视频

ios - 暂停/恢复时实时AVAssetWriter同步音频和视频

ios - 在后台使用 iBeacon 或 CoreBluetooth 识别 iOS 设备

ios - 如何从 firsTableViewController 获取单元格的名称并将其显示在 secendView 上?

objective-c - objective-c : Displaying Unicode characters

iphone - 平均多个 UIImages

ios - 为什么 UIImagePNGRepresentation 返回的数据大于(10 倍)图像的原始数据大小

ios - 照片的JPEG NSData转UIImage转JPEG大小完全不同