首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CALayers到PNG:奇怪的颜色变化

CALayers到PNG:奇怪的颜色变化
EN

Stack Overflow用户
提问于 2018-10-16 17:01:55
回答 1查看 39关注 0票数 0

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

下面是我的代码:

我将视图放入上下文中。

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

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

我想这与colorspace有关。但是我不知道我应该把genericRGBLinear改成什么来保持我的颜色。

你知道问题出在哪里吗?

EN

回答 1

Stack Overflow用户

发布于 2018-10-19 18:52:32

因此,解决方案是为相同颜色空间中的图层创建fillColor

代码语言:javascript
复制
    let colorSpace = CGColorSpace(name: CGColorSpace.genericRGBLinear)!

    layer.fillColor = CGColor(colorSpace: colorSpace, components: components)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52831744

复制
相关文章

相似问题

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