金属纹理未找到。

10

每次实现基于Metal的ImageView时,我都会遇到同样的问题。

let targetTexture = currentDrawable?.texture else{ return }

类型“MTLDrawable”的值没有成员“texture”

似乎苹果公司更改了一些 Metal API

这是我正在尝试使用的完整函数:

func renderImage()
{
    guard let
        image = image,
        let targetTexture = currentDrawable?.texture else{return}

    let commandBuffer = commandQueue.makeCommandBuffer()

    let bounds = CGRect(origin: CGPoint.zero, size: drawableSize)

    let originX = image.extent.origin.x
    let originY = image.extent.origin.y

    let scaleX = drawableSize.width / image.extent.width
    let scaleY = drawableSize.height / image.extent.height
    let scale = min(scaleX, scaleY)

    let scaledImage = image
        .applying(CGAffineTransform(translationX: -originX, y: -originY))
        .applying(CGAffineTransform(scaleX: scale, y: scale))

    ciContext.render(scaledImage,
                     to: targetTexture,
                     commandBuffer: commandBuffer,
                     bounds: bounds,
                     colorSpace: colorSpace)

    commandBuffer.present(currentDrawable!)

    commandBuffer.commit()
}

1
展示你的 currentDrawable 变量的声明和赋值。根据错误提示,它的类型是 MTLDrawable,实际上并没有 texture 属性。我想你可能在想 CAMetalDrawable,它有这个属性。 - Ken Thomases
@KenThomases 但是在每个实现中,Metal Kit视图都是以这种方式完成的,并且没有办法从MTLDrawable调用CAMetalDrawable。 - Діма Комар
你正在实现 MTKView 的子类吗? - Ken Thomases
2个回答

21

在执行系统和xcode更新后,我也遇到了同样的问题。事实证明,在更新过程中,xcode将构建目标切换到模拟器上了。一旦我将目标切换回设备上,所有内容就都编译成功了。


3
它起作用了,谢谢。但为什么它被制作得如此愚蠢,这是一个问题要问苹果公司。 - Діма Комар

0

金属不适用于模拟器,但如果你只想让构建通过并关注应用程序的其他方面,你可以尝试做类似于这样的事情: 对于那些仍然想在模拟器上运行的人,至少有一个解决方法可以使其运行和编译(虽然没有金属功能)

https://navoshta.com/metal-camera-bonus-running-simulator/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接