0

我想用另一种颜色改变 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];
}

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

4

1 回答 1

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 colr 组合中获取触摸点的颜色代码。试试看。

它会帮助你。

于 2013-10-15T06:20:23.633 回答