I have an array with colors as its objects. I want to create an image from it. In fact, what happens, I take the pixel value of each pixel in the image, modify it and save my object in the variable array. Now I want to draw an image from this. How to do it?? ANY idea ???
-(UIImage*)modifyPixels:(UIImage*)originalImage {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSMutableArray *result =[[NSMutableArray alloc]init];
int width = img.size.width;
int height = img.size.height;
originalImage = imageView.image;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc (height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height),img.CGImage);
CGContextRelease(context);
int byteIndex = 0;
for (int xx=0;xx<width;xx++){
for (int yy=0;yy<height;yy++){
NSLog(@"(%d,%d)",xx,yy);
NSLog(@"Alpha 255-Value is: %u", rawData[byteIndex + 3]);
NSLog(@"Red 255-Value is: %u", rawData[byteIndex]);
NSLog(@"Green 255-Value is: %u",rawData[byteIndex + 1]);
NSLog(@"Blue 255-Value is: %u",rawData[byteIndex + 2]);
CGFloat red = (rawData[byteIndex]+rawData[byteIndex + 1]+rawData[byteIndex + 2]) / 3;
CGFloat green = red;
CGFloat blue = red;
CGFloat alpha = 255;
byteIndex += 4;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
}
UIImage *newImage;
return newImage;
[pool release];
}
source
share