我的任务是将我的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。
我想这与colorspace有关。但是我不知道我应该把genericRGBLinear改成什么来保持我的颜色。
你知道问题出在哪里吗?
发布于 2018-10-19 18:52:32
因此,解决方案是为相同颜色空间中的图层创建fillColor
let colorSpace = CGColorSpace(name: CGColorSpace.genericRGBLinear)!
layer.fillColor = CGColor(colorSpace: colorSpace, components: components)https://stackoverflow.com/questions/52831744
复制相似问题