Reputation: 21
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);
}
Upvotes: 2
Views: 931
Reputation: 6856
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];
Upvotes: 1
Reputation: 33592
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.
Upvotes: 0