CAMetalLayer drawable texture is weird on some devices

懵懂的女人 提交于 2020-01-04 02:32:22

问题


I am using the below code to get and append a pixel buffer from a metal layer. On some non specific devices the output looks like below and the drawable textures pixelFormat is .invalid

static func make(with currentDrawable: CAMetalDrawable, usingBuffer pool: CVPixelBufferPool) -> (CVPixelBuffer?, UIImage) {

      let destinationTexture = currentDrawable.texture

      var pixelBuffer: CVPixelBuffer?
      _ = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBuffer)
      if let pixelBuffer = pixelBuffer {
        CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))
        let region = MTLRegionMake2D(0, 0, Int(currentDrawable.layer.drawableSize.width), Int(currentDrawable.layer.drawableSize.height))

        let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)

        let tempBuffer = CVPixelBufferGetBaseAddress(pixelBuffer)
        destinationTexture.getBytes(tempBuffer!, bytesPerRow: Int(bytesPerRow), from: region, mipmapLevel: 0)

        let image = imageFromCVPixelBuffer(buffer: pixelBuffer)
        CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))
        return (pixelBuffer, image)
      }
      return (nil, UIImage())
    }


static func imageFromCVPixelBuffer(buffer: CVPixelBuffer) -> UIImage {

      let ciimage = CIImage(cvPixelBuffer: buffer)
      let cgimgage = context.createCGImage(ciimage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(buffer), height: CVPixelBufferGetHeight(buffer)))

      let uiimage = UIImage(cgImage: cgimgage!)

      return uiimage
    }

Does anybody have any idea why this happens and how to prevent it?

There are several more feedback from people experiencing this can be found here: https://github.com/svtek/SceneKitVideoRecorder/issues/3

来源:https://stackoverflow.com/questions/46146593/cametallayer-drawable-texture-is-weird-on-some-devices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!