当我将MTKView的内容捕获成UIImage时,生成的图像质量会有所不同,如下所示:
我用以下代码生成UIImage:
let kciOptions = [kCIContextWorkingColorSpace: CGColorSpace(name: CGColorSpace.sRGB)!,
kCIContextOutputPremultiplied: true,
kCIContextUseSoftwareRenderer: false] as [String : Any]
let lastDrawableDisplayed = self.currentDrawable! // needed to hold the last drawable presented to screen
drawingUIView.image = UIImage(ciImage: CIImage(mtlTexture: lastDrawableDisplayed.texture, options: kciOptions)!)
因为我没有修改ciImage的方向(.oriented(CGImagePropertyOrientation.downMirrored)),所以生成的图像是倒置的,就像上面的图片所示。我保留了镜像方向,这样我就可以指出两个图像捕获之间的颜色差异。
无论我如何更改kciOptions参数(比如将颜色空间改为灰度),我都没有看到生成的UIImage有任何变化,它看起来比原始图像要暗淡/不鲜艳得多。是否有人有任何建议,可以准确地捕捉我在MTKView上绘制的内容到一个UIImage中?任何建议都将不胜感激。
以下是我的MTKView设置,可能与此相关:
let renderPipelineDescriptor = MTLRenderPipelineDescriptor()
renderPipelineDescriptor.vertexFunction = vertexProgram
renderPipelineDescriptor.sampleCount = self.sampleCount
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm
renderPipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
renderPipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
renderPipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
self.isOpaque = false // makes MTKView bg transparent