【问题标题】:CALayers to PNG: strange color changesCALayers 到 PNG:奇怪的颜色变化
【发布时间】:2018-10-16 09:01:54
【问题描述】:

我的任务是将我的 CALayer 树渲染为 PNG。当我这样做时,我得到的图像通常还可以,但颜色略有变化。

这是我的代码:

我将视图渲染到上下文中

   let contentsScale = view.layer!.contentsScale
        let width = Int(view.frame.width * contentsScale)
        let height = Int(view.frame.height * contentsScale)



    let bytesPerRow = width * 4
    let context = CGContext(
        data: nil,
        width: width,
        height: height,
        bitsPerComponent: 8,
        bytesPerRow: bytesPerRow,
        space: CGColorSpace(name: CGColorSpace.genericRGBLinear)! ,
        bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
        )!

    if needsAntialiased {
        context.setShouldAntialias(false)
    }

    view.layer!.render(in: context)

然后我将上下文保存到 PNG:

let image = context.makeImage()!

 guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypePNG, 1, nil) else { return false }

        CGImageDestinationAddImage(destination, image, nil)
        return CGImageDestinationFinalize(destination)

在某些时候我的颜色会改变。应该用 B30000 填充的层变成 870000。

我想这与色彩空间有关。但我不知道应该更改 genericRGBLinear 的什么来保留我的颜色。

有什么想法可以解决问题吗?

【问题讨论】:

    标签: png core-animation cgcontext


    【解决方案1】:

    所以解决方案是在相同的颜色空间中为图层创建填充颜色

        let colorSpace = CGColorSpace(name: CGColorSpace.genericRGBLinear)!
    
        layer.fillColor = CGColor(colorSpace: colorSpace, components: components)
    

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 2017-12-20
      • 1970-01-01
      • 2016-05-30
      • 2016-06-05
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多