I've been trying a ton of different options and tricks for getting a UIImage to build properly from a CIImage. But, every time I create the CIImage its colors seem to be inverted.
The most recent code I have is:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"C4Sky" withExtension:@"png"];
CIImage *c = [CIImage imageWithContentsOfURL:url];
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];
Which produces the following image:
However, building an image like this:
UIImage *i = [UIImage imageNamed:@"C4Sky"];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];
... produces the following image:
I've tried a lot of different ways of constructing the CIImage.
- https://mcmap.net/q/139081/-having-trouble-creating-uiimage-from-ciimage-in-ios5
- http://www.raywenderlich.com/5689/beginning-core-image-in-ios-5
- [CIImage imageWithData:UIImagePNGRepresentation(img)];
- almost every other CIImage constructor...
Is this happening because the pixel format in iOS for a CIImage is ARGB? (It seems this is the only format possible on iOS6 right now)
If so, how can I swap pixel formats get this CIImage to look normal?
I've created a project repo on git showing all the major different ways I've tried (not including variations of each). There are 6 methods, the first 5 failing:
https://github.com/C4Code/CIImageTest
The last method works, but it's really dirty:
-(void)test6 {
UIImage *img = [UIImage imageNamed:@"C4Sky"];
CGContextRef bitmapContext = NULL;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
CGSize size = img.size;
bitmapBytesPerRow = (size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * size.height);
bitmapData = malloc( bitmapByteCount );
bitmapContext = CGBitmapContextCreate(bitmapData, size.width, size.height,8,bitmapBytesPerRow,
CGColorSpaceCreateDeviceRGB(),kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(bitmapContext, (CGRect){CGPointZero,size}, img.CGImage);
CGImageRef imgRef = CGBitmapContextCreateImage(bitmapContext);
CIImage *cii = [CIImage imageWithCGImage:imgRef];
UIImage *finalImage = [UIImage imageWithCIImage:cii];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:finalImage];
[self.canvas addSubview:uiiv];
//works but it's dirrrrrrrty
}
I am drawing from a UIImage into my own graphics context, and because this works it has me believe that there might be a bug with CIImage's native implementation. That is, creating a CIImage from a UIImage doesn't work... Again, the only thing I can think of is that the CIImage is in ARGB space, and the UIImage isn't... But, I don't know how to get around this?
Is this a bug?