user440096
user440096

Reputation:

Changing the color of an image programmatically

I've a image that's cream in color and I want to be able to give it a blueish(for example) color on the fly without faking the color change by using another image.

Is this kind of thing catered for in iOS? Anybody able to offer some pointers to implementing this kind of thing?

Many Thanks, -Code

Upvotes: 1

Views: 1794

Answers (2)

jaym
jaym

Reputation: 1263

If you are expert of RGB, You can do anything with image using CGImage. You can go through each pixel and convert creem pixel to blueish!

Below is for your reference only, don't consider it as Answer.

-(UIImage *) getNewImageFromImage:(UIImage *)image {
    const int RED = 1, GREEN = 2, BLUE = 3;

    CGRect imageRect = CGRectMake(0, 0, image.size.width*2, image.size.height*2);

    int width = imageRect.size.width, height = imageRect.size.height;

    uint32_t * pixels = (uint32_t *) malloc(width*height*sizeof(uint32_t));
    memset(pixels, 0, width * height * sizeof(uint32_t));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImage]);

    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            uint8_t * rgbaPixel = (uint8_t *) &pixels[y*width+x];
            rgbaPixel[RED] = <DO_SOMETHING>;
            rgbaPixel[GREEN] = <DO_SOMETHING>;
            rgbaPixel[BLUE] = <DO_SOMETHING>;
        }
    }

    CGImageRef newImage = CGBitmapContextCreateImage(context);

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(pixels);

    UIImage * resultUIImage = [UIImage imageWithCGImage:newImage];
    CGImageRelease(newImage);

    return resultUIImage;
}

Upvotes: 2

CarlJ
CarlJ

Reputation: 9481

Maybe GPUImage could solve your problem:

The GPUImage framework is a BSD-licensed iOS library that lets you apply GPU-accelerated filters and other effects to images, live camera video, and movies. In comparison to Core Image (part of iOS 5.0), GPUImage allows you to write your own custom filters, supports deployment to iOS 4.0, and has a simpler interface. However, it currently lacks some of the more advanced features of Core Image, such as facial detection.

Upvotes: 3

Related Questions