从MTKView创建的UIImage导致颜色/不透明度差异 [英] UIImage created from MTKView results in color/opacity differences

查看:171
本文介绍了从MTKView创建的UIImage导致颜色/不透明度差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将MTKView的内容捕获到UIImage中时,生成的图像在质量上会有所不同,如下所示:

When I capture the contents of an MTKView into a UIImage, the resulting image looks qualitatively different, as shown below:

我用来生成UIImage的代码如下:

The code I use to generate the UIImage is as follows:

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方向(.directional(CGImagePropertyOrientation.downMirrored)),因此生成的图像是上下颠倒的,如上图所示.我将镜像方向保持不变,以便指出两个图像捕获之间的颜色差异.

Since I don't modify the ciImage orientation (.oriented(CGImagePropertyOrientation.downMirrored)) the resulting image is upside down, as shown in the image above. I leave the mirrored orientation as is so I can point out the color differences between the two image captures.

无论我如何更改kciOptions参数(例如,甚至将色彩空间更改为灰度),我都看不到结果UIImage的任何变化,UIImage看起来比原始图像暗淡得多.有人对我如何将MTKView上绘制的内容准确捕获到UIImage有什么建议吗?任何建议将不胜感激.

No matter how I change the kciOptions parameters, (say, even changing the colorspace to grayscale) I'm not seeing any changes in the resulting UIImage, which appears much more dim/desaturated than the original. Does anybody have any suggestions for how I can accurately capture what I'm drawing on MTKView to an UIImage? Any suggestions would be much appreciated.

以下是我的MTKView设置,可能证明是相关的:

Below are my MTKView settings which may prove relevant:

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

推荐答案

您的CGColorSpace.sRGB,但是renderPipelineDescriptor的pixelFormat是.bgra8Unorm.尝试将该行更改为:

Your CGColorSpace is .sRGB but your renderPipelineDescriptor's pixelFormat is .bgra8Unorm. Try changing that line to:

renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm_srgb

这篇关于从MTKView创建的UIImage导致颜色/不透明度差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆