I changed ur code like this
This code fill the image with green color only.
Can we fill the image with different color?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIColor *greenColor,
blueColor,redColor;
UIColor *theColor1;
if(button == @"green" || button ==@"red")
{
NSLog(@"I am Touch");
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == baseImage1)
{
NSLog(@"touch");
image1=baseImage1;
}
if([touch view] == baseImage)
{
NSLog(@"baseImage");
image1=baseImage;
}
NSLog(@"image=%@",image1);
//UIColor *greenColor;
CGImageRef imageRef = self.image1.image.CGImage;
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
size_t imageWidth = CGImageGetWidth(imageRef);
size_t imageHeight = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
//CGColorSpaceRef colorSpace =CGColorSpaceGetNumberOfComponents(imageRef);
CFDataRef dr = CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
UInt8* dataPtr = (UInt8*)CFDataGetBytePtr(dr);
int max = bytesPerRow * imageHeight;
for (int i = 0; i < max; i++)
{
if (0 != dataPtr[i + 3])
{
//NSInteger res=dataPtr[i + 3];
//NSLog(@"dataPtr[i + 3]=%d",res);
dataPtr[i++]=(int)redColor;
}
else
i += 3;
}
CGContextRef ref = CGBitmapContextCreate(dataPtr, imageWidth, imageHeight, bitsPerComponent, bytesPerRow, colorSpace,kCGImageAlphaNoneSkipLast);//To set the new image
if (nil == ref)
NSLog(@"Error creating context!");
CGImageRef imageRefNew = CGBitmapContextCreateImage(ref);
UIImage *img = [UIImage imageWithCGImage:imageRefNew];
self.image1.image = img;
CFRelease(imageRefNew);
CFRelease(ref);
CFRelease(colorSpace);
CFRelease(dr);
return self;
}
}
judy