Coloring an image
Hi i need to color an image.I refered with documents and wrote the below code,
My problem is,i ask 2 fill with red color,but the code fill my image with green color .and if i touch again the image.then the color is change to light green.if again i touch it wil become blank.
Whats the problem in my code?
- (void)touchesBegan

NSSet *)touches withEvent

UIEvent *)event
{
UIColor *greenColor, *blueColor,*redColor;
if(button == @"green" || button ==@"red")
{
NSLog(@"I am Touch");
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == baseImage1)
{
NSLog(@"touch");
//CGPoint location = [touch locationInView:self];
//baseImage.center = location;
image1=baseImage1;
}
if([touch view] == baseImage)
{
NSLog(@"baseImage");
//CGPoint location = [touch locationInView:self];
//baseImage.center = location;
image1=baseImage;
}
if([touch view] == buton1)
{
NSLog(@"buton1");
//CGPoint location = [touch locationInView:self];
//baseImage.center = location;
theColor=greenColor;
}
if([touch view] == buton2)
{
NSLog(@"buton2");
//CGPoint location = [touch locationInView:self];
//baseImage.center = location;
theColor=redColor;
}
NSLog(@"image=%@",image1);
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 ;
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])
{
dataPtr[i++]=(UInt8*)redColor;
}
else
i += 3;
}
CGContextRef ref = CGBitmapContextCreate(dataPtr, imageWidth, imageHeight, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);//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