TextureView

TextureView 是一个 Swift 包,它提供了一种在 iOS 应用程序中高效显示 Metal 纹理的方法。 它提供了一个可定制的 UIView 子类,可以渲染具有各种内容模式的 Metal 纹理,并支持额外的渲染命令。

特性

要求

安装

Swift 包管理器

将以下内容添加到你的 Package.swift 文件中

dependencies: [
    .package(
        url: "https://github.com/eugenebokhan/TextureView.git",
        .upToNextMajor(from: "1.1.0")
    )
]

用法

基本设置

  1. 导入模块
import TextureView
  1. 创建一个 TextureView 实例
let device = MTLCreateSystemDefaultDevice()!
let textureView = try TextureView(device: device)
  1. 将视图添加到你的视图层级结构中
view.addSubview(textureView)
  1. 设置要显示的纹理
textureView.texture = yourMTLTexture

自定义

纹理内容模式

你可以更改纹理在视图中的显示方式

textureView.textureContentMode = .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
)

高级用法

性能注意事项

许可证

TextureView 在 MIT 许可证下获得许可。