如何用其他颜色替换UIImage的颜色

时间:2013-10-14 12:57:41

标签: ios uiimageview uiimage uicolor

我想用另一种颜色改变UIImage的颜色,首先我要触摸图像并检测触摸像素的颜色,然后想要用另一种颜色替换触摸像素的颜色。

我有以下代码,其中我试图更改触摸的像素颜色,但其返回的透明图像。

- (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];
}

这里我试图让触摸的颜色透明,但如果我想将其更改为黄色,那么我需要更改我的代码?

1 个答案:

答案 0 :(得分:2)

从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 .

您可以在RGB颜色组合中获得触摸点的颜色代码。试试吧。

它会帮助你。