当我将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 orientation (.oriented(CGImagePropertyOrientation.downMirrored)),因此生成的图像是颠倒的,如上图所示。我保留镜像方向不变,这样我就可以指出两个图像捕获之间的颜色差异。
无论我如何更改kciOptions参数(例如,甚至将色彩空间更改为灰度),我都看不到结果UIImage中有任何变化,它看起来比原始的更加暗淡/不饱和。有没有人有什么建议可以让我在UIImage上准确地捕捉到我在MTKView上绘制的内容?任何建议都将不胜感激。
以下是我的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
发布于 2018-06-14 07:00:51
您的CGColorSpace
是.sRGB
,但renderPipelineDescriptor的pixelFormat是.bgra8Unorm
。尝试将该行更改为:
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm_srgb
发布于 2018-06-13 19:18:28
let context = CIContext()
let texture = metalView.currentDrawable!.texture
let cImg = CIImage(mtlTexture: texture, options: nil)!
let cgImg = context.createCGImage(cImg, from: cImg.extent)!
let uiImg = UIImage(cgImage: cgImg)
https://stackoverflow.com/questions/49224155
复制相似问题