title: CALayers to PNG: strange color changes
body: <p>My task is to render my CALayer tree to PNG. When I do this I get the image which is generally OK, but the colors slightly change.</p>
  
  <p>Here's my code:</p>
  
  <p>I render the view into context</p>
  
  <pre><code>   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)
  </code></pre>
  
  <p>And then I save the context into PNG:</p>
  
  <pre><code>let image = context.makeImage()!
  
   guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypePNG, 1, nil) else { return false }
  
          CGImageDestinationAddImage(destination, image, nil)
          return CGImageDestinationFinalize(destination)
  </code></pre>
  
  <p>At some point my colors change. The layer that should be filled with B30000 becomes 870000.</p>
  
  <p>I suppose it has something to do with colorspace. But I don't know what should I change genericRGBLinear for to preserve my colors.</p>
  
  <p>Any ideas where the problem can be?</p>