ios - 将 UIView 内容渲染到 MTLTexture
问题描述
我有一个(动画)UIView-Hierarchy,我想定期将 UIView 内容呈现到 MTLTexture 中以供进一步处理。
我尝试过的是子类化我的父 UIView 和
override public class var layerClass: Swift.AnyClass {
return CAMetalLayer.self
}
但是来自 nextDrawable() 的纹理是黑色的,并且不显示视图内容。
任何想法如何获得包含视图内容的 MTLTexture ?
解决方案
感谢Matthijs Hollemanns用一些代码为我指明了正确的方向,我提出了以下 UIView 扩展,它在 iPhone8plus 上以每帧约 12 毫秒的时间完成了全屏分辨率的工作。
extension UIView {
func takeTextureSnapshot(device: MTLDevice) -> MTLTexture? {
let width = Int(bounds.width)
let height = Int(bounds.height)
if let context = CGContext(data: nil,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: 0,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue),
let data = context.data {
layer.render(in: context)
let desc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .rgba8Unorm,
width: width,
height: height,
mipmapped: false)
if let texture = device.makeTexture(descriptor: desc) {
texture.replace(region: MTLRegionMake2D(0, 0, width, height),
mipmapLevel: 0,
withBytes: data,
bytesPerRow: context.bytesPerRow)
return texture
}
}
return nil
}
}
推荐阅读
- javascript - 无法在 HTML 页面上查看 JavaScript 函数的结果
- docker - 拉图像然后运行与直接运行之间的区别
- angular - Angular 7 异常行为当用户导航回登录时,如果他使用浏览器的前进键,他可以访问其他组件
- unity3d - 与外部蓝牙设备配对时如何解决 Unity 中的 32feet.NET 错误?
- java - RestTemplate 使用动态键映射 JSON 键值对对象
- c# - 如何返回此页面的先前状态
- r - 在 r 中创建绘图函数
- javascript - 如何使表格行滑动淡入/淡出连续
- mysql - 修改 SELECT 查询显示输出
- arrays - 如何将图像(角度 6)发布到 Web Api 2?