I have a CGImageRef in RGB obtained from ZXing barcode library, however, I need this file in CMYK Color Space. I made several attempt to get this using Cocoa, unsuccessfully.
The closest I could get is to get the picture in CMYK, but it lost its transparency during the conversion. The transparency has been converted to white somehow.
Here is the code I used:
NSImage * sourceImage = [[NSImage alloc] initWithContentsOfFile:finalPath.path];
NSBitmapImageRep * sourceImageRep = [[sourceImage representations] objectAtIndex:0];
NSColorSpace * targetColorSpace = [NSColorSpace genericCMYKColorSpace];
NSBitmapImageRep * targetImageRep = [sourceImageRep bitmapImageRepByConvertingToColorSpace:targetColorSpace
renderingIntent:NSColorRenderingIntentPerceptual];
NSData * data = [targetImageRep representationUsingType: NSTIFFFileType properties: nil];
[data writeToFile:finalPath.path atomically: NO];
Result: File converted to CMYK but the barcode lost its transparency.