首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从MTKView创建的UIImage会导致颜色/不透明度差异

从MTKView创建的UIImage会导致颜色/不透明度差异
EN

Stack Overflow用户
提问于 2018-03-12 03:29:10
回答 2查看 1.7K关注 0票数 6

当我将MTKView的内容捕获到UIImage中时,得到的图像看起来有本质上的不同,如下所示:

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

代码语言:javascript
复制
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设置,它们可能是相关的:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-14 07:00:51

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

代码语言:javascript
复制
renderPipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm_srgb
票数 2
EN

Stack Overflow用户

发布于 2018-06-13 19:18:28

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49224155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档