1

I want to change the color of all pixels in UIImage. This code successfuly change the colour for a single point but when I run it in a loop for whole image then my Image disappears. First I read the pixel colour value then I apply changes to the colour of this pixel. Also loop runs for i's 0 value only. Below is the code

    UIImage *grayImage=[UIImage imageWithCGImage:source.CGImage];
            for(int i=0;i<imageToTest.frame.size.width;i++){
                for(int j=0;j<imageToTest.frame.size.height;j++){
                    NSLog(@"%d",i);
                    CGPoint point=CGPointMake((CGFloat) i,(CGFloat) j);
                    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, source.size.width, source.size.height), point)) {
                        return nil;
                    }

                    NSInteger pointX = trunc(point.x);
                    NSInteger pointY = trunc(point.y);
                    CGImageRef cgImage = source.CGImage;
                    NSUInteger width = CGImageGetWidth(cgImage);
                    NSUInteger height = CGImageGetHeight(cgImage);
                    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                    int bytesPerPixel = 4;
                    int bytesPerRow = bytesPerPixel * 1;
                    NSUInteger bitsPerComponent = 8;
                    unsigned char pixelData[4] = { 0, 0, 0, 0 };
                    CGContextRef context = CGBitmapContextCreate(pixelData, 
                                                                 1,
                                                                 1,
                                                                 bitsPerComponent, 
                                                                 bytesPerRow, 
                                                                 colorSpace,
                                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
                    CGColorSpaceRelease(colorSpace);
                    CGContextSetBlendMode(context, kCGBlendModeCopy);

                        // Draw the pixel we are interested in onto the bitmap context
                    CGContextTranslateCTM(context, -pointX, -pointY);
                    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
                    CGContextRelease(context);

                        // Convert color values [0..255] to floats [0.0..1.0]
                    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
                    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
                    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
                    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
                    UIColor *currentPixelColor;
                    CGFloat Answer=sliderValue/10;
                    red=red*Answer;
                    green=green*Answer;
                    blue=blue*Answer;
                    alpha=alpha*Answer;
                    currentPixelColor=[UIColor colorWithRed:red green:green blue:blue alpha:alpha];


                    int width2 = source.size.width;
                    int height2 = source.size.height;

                    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();    
                    CGContextRef context2 = CGBitmapContextCreate (nil,
                                                                   width2,
                                                                   height2,
                                                                   8,
                                                                   0,
                                                                   colorSpace2,
                                                                   kCGImageAlphaPremultipliedFirst);

                    CGColorSpaceRelease(colorSpace2);

                    if (context2 == NULL) {
                        return nil;
                    }

                    CGContextDrawImage(context2,
                                       CGRectMake(0, 0, width2, height2), grayImage.CGImage);

                    CGContextSetFillColorWithColor(context2, currentPixelColor.CGColor);
                    CGContextFillRect(context2, CGRectMake(point.x, point.y, 1, 1));

                    grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context2)];
                    CGContextRelease(context2);

                }

            }


            return grayImage;

}
Idrees Ashraf
  • 1,363
  • 21
  • 38

2 Answers2

1

I suspect you are running into this condition and returning nil (hence your image disappears):

if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, source.size.width, source.size.height), point)) {
    return nil;
}

I think your iterations should be over source.size.width and source.size.height instead of the containing view's dimensions.

On a side note, you are probably iterating at a level to high. It would probably be possible (and certainly more efficient) to only create on CGContextRef and apply all your changes. Honestly I haven't looked at your algorithm close enough but you should try to take everything that doesn't need to be reiterated over and over out of your loop.

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • kindly suggest me the easy way to do this task because it is not optimal solution to change each pixel colour – Idrees Ashraf May 25 '12 at 13:13
  • This [post](http://stackoverflow.com/questions/7122221/how-to-implement-fast-image-filters-on-ios-platform) might help you – mprivat May 25 '12 at 13:38
0

You should look at using Core Image filters to do your color adjustments. They are very fast, and pretty easy to use. Erica Sadun's book "The iOS 5 Developer's Cookbook" includes a chapter on using CI that is very clear and easy to use. It also includes a work-around for an OS bug that prevents you from getting your modified image back from Core Image to a UIImage.

Duncan C
  • 128,072
  • 22
  • 173
  • 272