Colors inverted when creating UIImage from CIImage (iOS 6.0)

I tried a ton of different options and tricks to get UIImage to properly build from CIImage. But every time I create 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 creates the following image:

The inverted color image.

However, creating the image as follows:

UIImage *i = [UIImage imageNamed:@"C4Sky"];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

... creates the following image:

enter image description here

I tried many different ways to create CIImage.

Is this because the pixel format in iOS for CIImage is ARGB? (This seems to be the only format you can use on iOS6 right now)

, , CIImage ?



git, , ( ). 6 , 5 :

https://github.com/C4Code/CIImageTest

, :

-(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 dirrrrrrrty
}

UIImage , , , , CIImage. CIImage UIImage ... , , , , CIImage ARGB, UIImage ... ?

?

+5
3

:

//Grab the CIImage directly from the UIImage
UIImage *img = [UIImage imageNamed:@"C4Sky.png"];
CIImage *c = img.CIImage;
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

//Maybe you need to set your own color space?
UIImage *img = [UIImage imageNamed:@"C4Sky.png"];
CIImage *c = [CIImage imageWithCGImage:img.CGImage options:@{kCIImageColorSpace: kCGColorSpaceModelRGB}];
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

-jt

+6

. UIImage CIImage - :

- (UIImage *)newUIImageFromCIImage:(CIImage *)image
{
    CGImageRef newImageRef = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:[image extent]]; // you can cache CIContext as it a little slow to create
    UIImage *newImage = [[UIImage alloc] initWithCGImage:newImageRef];
    CGImageRelease(newImageRef);

    return newImage;
}

.

+2

Check out this tutorial. You must apply filters before they look good. Hope this helps!

rachel :)

eg. (only in case of breaking links)

CIImage *beginImage = [CIImage imageWithContentsOfURL:fileNameAndPath];

CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
                          keysAndValues: kCIInputImageKey, beginImage,
                @"inputIntensity", @0.8, nil];
CIImage *outputImage = [filter outputImage];


CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];


UIImage *newImage = [UIImage imageWithCGImage:cgimg]; 
self.imageView.image = newImage;


CGImageRelease(cgimg);
+1
source

All Articles