- (CGImageRef) CreateARGBImageWithABGRImage: (CGImageRef) abgrImageRef {
// the data retrieved from the image ref has 4 bytes per pixel (ABGR).
CFDataRef abgrData = CGDataProviderCopyData(CGImageGetDataProvider(abgrImageRef));
UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(abgrData);
int length = CFDataGetLength(abgrData);
// abgr to rgba
// swap the blue and red components for each pixel...
UInt8 tmpByte = 0;
for (int index = 0; index < length; index+= 4) {
tmpByte = pixelData[index + 1];
pixelData[index + 1] = pixelData[index + 3];
pixelData[index + 3] = tmpByte;
}
// grab the bgra image info
size_t width = CGImageGetWidth(abgrImageRef);
size_t height = CGImageGetHeight(abgrImageRef);
size_t bitsPerComponent = CGImageGetBitsPerComponent(abgrImageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(abgrImageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(abgrImageRef);
CGColorSpaceRef colorspace = CGImageGetColorSpace(abgrImageRef);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(abgrImageRef);
// create the argb image
CFDataRef argbData = CFDataCreate(NULL, pixelData, length);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(argbData);
CGImageRef argbImageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow,
colorspace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault);
// release what we can
CFRelease(abgrData);
CFRelease(argbData);
CGDataProviderRelease(provider);
// return the pretty new image
return argbImageRef;
}
So there you have it. I hope this helps someone else. Suggestions for improvements on this are certainly welcome.