简体   繁体   English

如何在 iOS 中将 PNG 图像转换为 CMYK?

[英]How to convert PNG image to CMYK in iOS?

I have a set of user-generated PNG images that I need to embed into print-ready PDFs.我有一组用户生成的 PNG 图像,我需要将它们嵌入到可打印的 PDF 中。 One of the constraints set by the printing company is that all images be in the CMYK colorspace.印刷公司设置的限制之一是所有图像都在 CMYK 色彩空间中。 How do I convert a PNG (or UIImage containing that PNG) to CMYK?如何将 PNG(或包含该 PNG 的 UIImage)转换为 CMYK? By default the colorspace is sRGB.默认情况下,色彩空间是 sRGB。

You should consider CGColorSpace class and CGColorSpaceCreateDeviceCMYK function.您应该考虑CGColorSpace类和CGColorSpaceCreateDeviceCMYK函数。 You can create a CGContex using this initializer and pass CMYK color space as a parameter to it.您可以使用初始值设定项创建CGContex并将 CMYK 颜色空间作为参数传递给它。 Then just draw a CGImage from that context and create UIImage from it.然后从该上下文中绘制一个CGImage从中创建 UIImage 。

Example:示例:

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)
}

According to https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203 "Supported Pixel Formats" table you must use根据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)

to correctly create a context正确创建上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM