从 MTKView 创建的 UIImage 导致 color/opacity 差异
UIImage created from MTKView results in color/opacity differences
当我将 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 有任何变化,它看起来比原始图像 dim/desaturated 多得多。有人对我如何准确地将我在 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
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)
您的 CGColorSpace
是 .sRGB
但您的 renderPipelineDescriptor 的 pixelFormat 是 .bgra8Unorm
。尝试将该行更改为:
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm_srgb
我在几个帖子中看到过这个问题,但没有明确的答案。这是我发现的:
首先,
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm
真的应该设置为 MTKView 的原生像素格式
renderPipelineDescriptor.colorAttachments[0].pixelFormat = self.colorPixelFormat
其次,当我设置 CIImage 的选项时:
let kciOptions = [kCIContextWorkingColorSpace: CGColorSpace(name: CGColorSpace.sRGB)!,
kCIContextOutputPremultiplied: true,
kCIContextUseSoftwareRenderer: false] as [String : Any]
无论我将 kCIContextWorkingColorSpace 设置为什么,无论我使用什么,我都没有看到任何视觉差异。我真正需要设置的 属性 叫做 KCIImageColorSpace。所以更新后的 kciOptions 看起来像:
let kciOptions = [kCIImageColorSpace: CGColorSpaceCreateDeviceRGB(),
kCIContextOutputPremultiplied: true,
kCIContextUseSoftwareRenderer: false] as [String : Any]
以与使用视图的本机像素格式类似的方式,调用 CGColorSpaceCreateDeviceRGB() 创建特定于正在使用的设备的 RGB 色彩空间。
当我将 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 有任何变化,它看起来比原始图像 dim/desaturated 多得多。有人对我如何准确地将我在 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
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)
您的 CGColorSpace
是 .sRGB
但您的 renderPipelineDescriptor 的 pixelFormat 是 .bgra8Unorm
。尝试将该行更改为:
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm_srgb
我在几个帖子中看到过这个问题,但没有明确的答案。这是我发现的:
首先,
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm
真的应该设置为 MTKView 的原生像素格式
renderPipelineDescriptor.colorAttachments[0].pixelFormat = self.colorPixelFormat
其次,当我设置 CIImage 的选项时:
let kciOptions = [kCIContextWorkingColorSpace: CGColorSpace(name: CGColorSpace.sRGB)!,
kCIContextOutputPremultiplied: true,
kCIContextUseSoftwareRenderer: false] as [String : Any]
无论我将 kCIContextWorkingColorSpace 设置为什么,无论我使用什么,我都没有看到任何视觉差异。我真正需要设置的 属性 叫做 KCIImageColorSpace。所以更新后的 kciOptions 看起来像:
let kciOptions = [kCIImageColorSpace: CGColorSpaceCreateDeviceRGB(),
kCIContextOutputPremultiplied: true,
kCIContextUseSoftwareRenderer: false] as [String : Any]
以与使用视图的本机像素格式类似的方式,调用 CGColorSpaceCreateDeviceRGB() 创建特定于正在使用的设备的 RGB 色彩空间。