10
votes

With every Implementation of Metal based ImageView I'm facing the same problem

let targetTexture = currentDrawable?.texture else{ return }

Value of type 'MTLDrawable' has no member 'texture'

Seems like apple has changed some metal api

here is the full function I'm tryong to use:

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()
}
2
Show the declaration and assignment of your currentDrawable variable. According to the error, it's of type MTLDrawable which does not, in fact, have a texture property. I imagine you were thinking of CAMetalDrawable, which does.Ken Thomases
@KenThomases but in every implementation Metal kit view was done in this way, and there no way to invoke CAMetalDrawable from MTLDrawableДіма Комар
Are you implementing a subclass of MTKView?Ken Thomases

2 Answers

21
votes

I had the same problem after performing a system and xcode update. Turns out during the update process, xcode switched the build target to the simulator. Once I switched the target back to the device it all compiled again.

0
votes

Metal doesn't work for simulators, but if you just want the build to pass and focus in other areas of your app you can try to do something similar to this: For those who still want to run on simulators, there's a workaround for at least getting it running and compile (no Metal functionality though)

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