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);
}