The Shape of Everything
A website mostly about Mac stuff, written by Gus Mueller
» Acorn
» Twitter
» Maybe Pizza?
» Code
» Archive
July 7, 2015

I recently had a need to write out an image from a CGImageRef to disk, but without a color space added to it. So I took to twitter, and Ken Ferry came to my rescue. I present the solution here, incase you should ever need it as well:

CGImageRef img = …
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
img = CGImageCreateCopyWithColorSpace(img, cs);
NSURL *url = [NSURL fileURLWithPath:@"/tmp/foo.jpeg"];
CGImageDestinationRef destRef =
    CGImageDestinationCreateWithURL((CFURLRef)url, kUTTypeJPEG, 1, nil);
CGImageDestinationAddImage(destRef, img, nil);
CGImageDestinationFinalize(destRef);
CGColorSpaceRelease(cs);
CGImageRelease(img);
CFRelease(destRef);

Thanks Ken!