2013-10-14 81 views
0

我想用另一種顏色改變UIImage的顏色,首先我想觸摸圖像並檢測所觸摸像素的顏色,然後想用另一種顏色替換所觸摸像素的顏色。如何用其他顏色替換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]; 
} 

這裏我試圖讓感動的顏色透明,但如果我想改變它爲黃色,那麼我需要改變我的代碼?

回答

2

調用此方法的形式uitouch移動...

-(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組合。嘗試一下。

它會幫助你。