0

I want to change color of UIImage with another color, firstly i want to touch the image and detect the color of the touched pixel and then want to replace color of touched pixel with another color.

I have following code in which i am trying to change touched pixel color but its returning transparent image.

- (UIColor *)colorAtPixel:(CGPoint)point
{
// Cancel if point is outside image coordinates
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
        return nil;
    }

// Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
// Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.CGImage;
    NSUInteger width = self.size.width;
    NSUInteger height = self.size.height;
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   int bytesPerPixel = 4;
   int bytesPerRow = bytesPerPixel * 1;
   NSUInteger bitsPerComponent = 8;
   unsigned char pixelData[4] = { 0, 0, 0, 0 };
   CGContextRef context = CGBitmapContextCreate(pixelData, 
                                             1,
                                             1,
                                             bitsPerComponent, 
                                             bytesPerRow, 
                                             colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);

    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);

    // Convert color values [0..255] to floats [0.0..1.0]
   red   = (CGFloat)pixelData[0] / 255.0f;
   green = (CGFloat)pixelData[1] / 255.0f;
   blue  = (CGFloat)pixelData[2] / 255.0f;
   alpha = (CGFloat)pixelData[3] / 255.0f;
   [self changeWhiteColorTransparent:imageview.img];
   return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
} 


-(void)changeWhiteColorTransparent: (UIImage *)image{
    CGImageRef rawImageRef = image.CGImage;
    const float colorMasking[6] = { red, 0, green, 0, blue, 0 };

   UIGraphicsBeginImageContext(image.size);
    CGImageRef maskedImageRef =  CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
   {
        //if in iphone
        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
        CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
   }

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(maskedImageRef);
   UIGraphicsEndImageContext();


   NSString *imagespath =[self createDirectoryInDocumentsFolderWithName:@"images"];
   NSFileManager *fileM = [NSFileManager defaultManager];
   NSArray *contents= [fileM contentsOfDirectoryAtPath:imagespath error:nil];
   NSString *savedImagePath = [imagespath stringByAppendingPathComponent:[NSString stringWithFormat:@"images%d.png",[contents count] + 1]];
   NSData *imageData = UIImagePNGRepresentation(result);
   [imageData writeToFile:savedImagePath atomically:NO];
}

here i an trying to make touched colour transparent but if i want to change it to yellow color then what i need to change i code?

287986
  • 93
  • 1
  • 12

1 Answers1

2

Call this method form uitouch move...

 -(void) getPixelColorAtLocation:(CGPoint)point
  { 
   unsigned char pixel[4] = {0};
   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
   CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

   [self.layer renderInContext:context];

  // NSLog(@"x- %f  y- %f",point.x,point.y);

  CGContextRelease(context);
   CGColorSpaceRelease(colorSpace);

   NSLog(@"RGB Color code :%d  %d  %d",pixel[0],pixel[1],pixel[2]);
}

 set this RGB value to your imageview .

You can get color code of touch point in RGB colr combination. try it.

it will help you.

Dev Patel
  • 290
  • 1
  • 9