【问题标题】:How to convert PNG image to CMYK in iOS?如何在 iOS 中将 PNG 图像转换为 CMYK?
【发布时间】:2017-03-12 21:35:17
【问题描述】:

我有一组用户生成的 PNG 图像,我需要将它们嵌入到可打印的 PDF 中。印刷公司设置的限制之一是所有图像都在 CMYK 颜色空间中。如何将 PNG(或包含该 PNG 的 UIImage)转换为 CMYK?默认情况下,颜色空间是 sRGB。

【问题讨论】:

    标签: ios colors uiimageview uiimage


    【解决方案1】:

    根据https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203“支持的像素格式”表,您必须使用

        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
    

    正确创建上下文

    【讨论】:

      【解决方案2】:

      您应该考虑CGColorSpace 类和CGColorSpaceCreateDeviceCMYK 函数。 您可以使用this 初始化程序创建CGContex,并将CMYK 颜色空间作为参数传递给它。然后从该上下文中绘制一个CGImage 并从中创建 UIImage。

      例子:

      func createCMYK(fromRGB image: UIImage) -> UIImage? {
          let size = image.size
          let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceCMYK()
          let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
          guard let context = CGContext(data: nil,
                                        width: Int(size.width),
                                        height: Int(size.height),
                                        bitsPerComponent: 8,
                                        bytesPerRow: 4 * Int(size.width),
                                        space: colorSpace,
                                        bitmapInfo: bitmapInfo.rawValue) else { return nil }            
          context.draw(image.cgImage!, in: CGRect(origin: .zero, size: size))
          guard let cgImage = context.makeImage() else { return nil }
          return UIImage(cgImage: cgImage)
      }
      

      【讨论】:

      • 它不起作用。它因“: CGBitmapContextCreate: unsupported parameter combination: set CGBITMAP_CONTEXT_LOG_ERRORS environment variable to see the details”而崩溃
      猜你喜欢
      • 2020-07-03
      • 2018-12-05
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2013-10-07
      • 2011-06-16
      相关资源
      最近更新 更多