TextureView 是一个 Swift 包,它提供了一种在 iOS 应用程序中高效显示 Metal 纹理的方法。 它提供了一个可定制的 UIView 子类,可以渲染具有各种内容模式的 Metal 纹理,并支持额外的渲染命令。
将以下内容添加到你的 Package.swift
文件中
dependencies: [
.package(
url: "https://github.com/eugenebokhan/TextureView.git",
.upToNextMajor(from: "1.1.0")
)
]
import TextureView
let device = MTLCreateSystemDefaultDevice()!
let textureView = try TextureView(device: device)
view.addSubview(textureView)
textureView.texture = yourMTLTexture
你可以更改纹理在视图中的显示方式
textureView.textureContentMode = .aspectFit
可用的模式是
.resize
: 拉伸或收缩纹理以填充视图.aspectFill
: 缩放纹理以填充视图,同时保持纵横比.aspectFit
: 缩放纹理以适应视图,同时保持纵横比更改视图的可绘制对象的像素格式
try textureView.setPixelFormat(.bgra8Unorm_srgb)
启用或禁用在视图边界更改时自动调整可绘制对象的大小
textureView.autoResizeDrawable = false
要绘制纹理,请在渲染循环中调用 draw
方法
let commandBuffer = commandQueue.makeCommandBuffer()!
textureView.draw(in: commandBuffer)
commandBuffer.commit()
你还可以提供额外的渲染命令
textureView.draw(
additionalRenderCommands: { encoder in
// Your additional render commands here
},
in: commandBuffer
)
CAMetalLayer
进行高效渲染。TextureView 在 MIT 许可证下获得许可。