Search code examples
objective-ccolorsuiimagecore-graphicspixel

Change color of UIImage by pixel array


I want to change color of my image but dont want to change alpha of image. I am using following code for change color in blue.

But i want to change image of all pixels array into perticuler RGB value.

Like I have to apply RGB value (R= 116 G=170 B= 243).

CGImageRef sourceImage = ImageView_Test.image.CGImage;

        CFDataRef theData;
        theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

        UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);

        int red = 0;
        int green = 1;
        int blue = 2;

        int dataLength = CFDataGetLength(theData);
        for (int index = 0; index < dataLength; index += 4)
        {
            if (pixelData[index + blue] - 80 > 0)
            {
                pixelData[index + red] = pixelData[index + blue] - 139;
                pixelData[index + green] = pixelData[index + blue] - 85;
            }
            else
            {
                pixelData[index + green] = 0;
                pixelData[index + red] = 0;
            }
        }

        CGContextRef context;
        context = CGBitmapContextCreate(pixelData,
                                        CGImageGetWidth(sourceImage),
                                        CGImageGetHeight(sourceImage),
                                        8,
                                        CGImageGetBytesPerRow(sourceImage),
                                        CGImageGetColorSpace(sourceImage),
                                        kCGImageAlphaPremultipliedLast);

        CGImageRef newCGImage = CGBitmapContextCreateImage(context);
        UIImage *newImage = [UIImage imageWithCGImage:newCGImage];
        ImageView_Test.image = newImage;
        CGContextRelease(context);
        CFRelease(theData);
        CGImageRelease(newCGImage);

Solution

  • I am using following method for change color of UIImage without affecting alpha of it.

    -(UIImage *)didImageColorchanged:(NSString *)name withColor:(UIColor *)color
    {
        UIImage *img = [UIImage imageNamed:name];
        UIGraphicsBeginImageContext(img.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [color setFill];
        CGContextTranslateCTM(context, 0, img.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetBlendMode(context, kCGBlendModeColorBurn);
        CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
        CGContextDrawImage(context, rect, img.CGImage);
        CGContextClipToMask(context, rect, img.CGImage);
        CGContextAddRect(context, rect);
        CGContextDrawPath(context,kCGPathFill);
        UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return coloredImg;
    }
    

    Ex:

    resultView.image =  [self didImageColorchanged:[UIImage imageNamed:@"xyz.png"] withColor:[UIColor redColor]];