2

i am new to iphone and i am making an app in which i want to change the image pixels . i am using uiimageview. i have done some of the work. the pixel data is change as i think so but it do not show the new updated image. here is my code

-(void)Change{
struct pixel {
  unsigned char r, g, b, a;
 };

 UIImage *myimage = [ UIImage imageNamed:@"iphone-wallpapaer-silver-apple.jpg"];

 NSUInteger numberOfRedPixels = 0;
 NSUInteger numberOfGreenPixels = 0;
 NSUInteger numberOfBluePixels = 0;
 NSUInteger numberOfAlphaPixels = 0;
 struct pixel* pixels = (struct pixel*) calloc(1, myimage.size.width * myimage.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap

        CGContextRef context = CGBitmapContextCreate(
              (void*) pixels,
              myimage.size.width,
              myimage.size.height,
              8,
              myimage.size.width * 4,
              CGImageGetColorSpace(myimage.CGImage),
              kCGImageAlphaPremultipliedLast
              );

        if (context != NULL)
        {
            // Draw the image in the bitmap

            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, myimage.size.width, myimage.size.height), myimage.CGImage);

            NSUInteger numberOfPixels = myimage.size.width * myimage.size.height;
   //NSLog(@"%d",numberOfPixels);
            while (numberOfPixels > 0) {
                if (pixels->r == 255) {
                    pixels->r = 0;
     //NSLog(@"%d",pixels->r);
     numberOfRedPixels++;
                }
    if(pixels->g == 255){
     pixels->g = 0;
     //NSLog(@"%d",pixels->g);
     numberOfGreenPixels ++;
    }
    if(pixels->b == 255){
     pixels->b = 0;
     //NSLog(@"%d",pixels->b);
     numberOfBluePixels ++;
    }
    if(pixels->a == 255){
     pixels->a = 0;
     numberOfAlphaPixels ++;
    }
                pixels++;
                numberOfPixels--;
   }

   context = CGBitmapContextCreate((void*)pixels,  
           myimage.size.width,
           myimage.size.height, 
           8,  
           myimage.size.width * 4,
           CGImageGetColorSpace(myimage.CGImage),
           kCGImageAlphaPremultipliedFirst );
UIImage *newImage   = [UIImage imageWithCGImage:context];
CGImageRef imageRef = CGBitmapContextCreateImage (newImage);   
}
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
huzfi
  • 21
  • 2

2 Answers2

1

Without seeing the rest of your code, you can try several things:

[self.myUIImageView setImage:newImage]; // actually sets the image into the view
[newImage release]; //cleanup memory.
[self needsDisplay]; //refresh the View

You shouldn't need the CGImageRef at the end.

Also, look here for suggestions on how to do this in an alternative manner.


[EDIT]

My suggestions was to not use CG at all.

NSData *pixelData = UIImagePNGRepresentation(self.UIImageView.UIImage); //psuedo-code

void* pixelBytes = [pixelData bytes];

// Take away the red pixel, assuming 32-bit RGBA
for(int i = 0; i < [pixelData length]; i += 4) {
    bytes[i] = 0; // red
    bytes[i+1] = bytes[i+1]; // green
    bytes[i+2] = bytes[i+2]; // blue
    bytes[i+3] = bytes[i+3]; // alpha
}
NSData* newPixelData = [NSData dataWithBytes:pixelBytes length:[pixelData length]];
UIImage* newImage = [UIImage imageWithData:newPixelData];
Community
  • 1
  • 1
Stephen Furlani
  • 6,794
  • 4
  • 31
  • 60
  • Thanks i have understand this and done something quite close to it. but the problem lies in the start. the image is not being created correctly by CGBitmapContextCreate method – huzfi Sep 23 '10 at 05:57
  • You can do your alternative math with `bytes[i]` above. `if(bytes[i]==255) { bytes[i] = 0; redPixels++};` or something. – Stephen Furlani Sep 23 '10 at 12:56
0
context = CGBitmapContextCreate((void*)pixels,

You don't need to create a second context (and in doing so, you're leaking the first context). I'm also not sure why you changed from AlphaPremultipliedLast to AlphaPremultipliedFirst.

UIImage *newImage   = [UIImage imageWithCGImage:context];

context is a CGContextRef, not a CGImageRef. That won't work.

CGImageRef imageRef = CGBitmapContextCreateImage (newImage);   

newImage is a UIImage*, not a CGContextRef. That won't work either.

tc.
  • 33,468
  • 5
  • 78
  • 96
  • Thanks i understand the points u mentioned and also already had made changes with newimage and imageref. now what prob i am getting is to load the image. when i create it with CGBitmapContextCreate and copy it to my disk to confrm. it shows a blank image :( – huzfi Sep 23 '10 at 05:56