2011-01-14 79 views
3

我使用不同的圖像,我想包括更改顏色選項。但我不能。任何身體幫助我?我如何更改iphone sdk中的uiimage顏色xcode

+0

你想用一種顏色「染」圖像嗎? 「改變顏色選項」是什麼意思?如果整個圖像是白色的,則爲 – Dunkelstern 2011-01-14 12:19:26

+0

。如果我選擇黑色按鈕,那麼整個圖像將是黑色的...等 – 2011-01-14 12:22:23

回答

8

如果您想要進行圖像着色,請參見kballard/MGImageUtilities中的UIImage + Tint.m。如果您想要批量更換顏色(例如,將圖像作爲輪廓處理並將整個顏色更改爲一個平面顏色),請參見mattgemmell/MGImageUtilities中的UIImage + Tint.m。

+0

凱文@謝謝分配,現在它的工作。我可以輕鬆地更改任何圖像的顏色。 – 2011-01-14 13:24:18

+0

使用一些非常分辨率的圖像時獲取內存警告。任何想法? – Akshay 2011-12-13 13:03:54

3

完成此操作的一種方法是對圖像進行去飽和處理,然後使用所需顏色在圖像上添加色調。

去色

-(UIImage *) getImageWithUnsaturatedPixelsOfImage:(UIImage *)image { 
    const int RED = 1, GREEN = 2, BLUE = 3; 

    CGRect imageRect = CGRectMake(0, 0, image.size.width*2, image.size.height*2); 

    int width = imageRect.size.width, height = imageRect.size.height; 

    uint32_t * pixels = (uint32_t *) malloc(width*height*sizeof(uint32_t)); 
    memset(pixels, 0, width * height * sizeof(uint32_t)); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); 

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImage]); 

    for(int y = 0; y < height; y++) { 
     for(int x = 0; x < width; x++) { 
      uint8_t * rgbaPixel = (uint8_t *) &pixels[y*width+x]; 
      uint32_t gray = (0.3*rgbaPixel[RED]+0.59*rgbaPixel[GREEN]+0.11*rgbaPixel[BLUE]); 

      rgbaPixel[RED] = gray; 
      rgbaPixel[GREEN] = gray; 
      rgbaPixel[BLUE] = gray; 
     } 
    } 

    CGImageRef newImage = CGBitmapContextCreateImage(context); 

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    free(pixels); 

    UIImage * resultUIImage = [UIImage imageWithCGImage:newImage scale:2 orientation:0]; 
    CGImageRelease(newImage); 

    return resultUIImage; 
} 

疊加隨着顏色

-(UIImage *) getImageWithTintedColor:(UIImage *)image withTint:(UIColor *)color withIntensity:(float)alpha { 
    CGSize size = image.size; 

    UIGraphicsBeginImageContextWithOptions(size, FALSE, 2); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    [image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0]; 

    CGContextSetFillColorWithColor(context, color.CGColor); 
    CGContextSetBlendMode(context, kCGBlendModeOverlay); 
    CGContextSetAlpha(context, alpha); 

    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(CGPointZero.x, CGPointZero.y, image.size.width, image.size.height)); 

    UIImage * tintedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return tintedImage; 
} 

如何對

//For a UIImageView 
yourImageView.image = [self getImageWithUnsaturatedPixelsOfImage:yourImageView.image]; 
yourImageView.image = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7]; 

//For a UIImage 
yourImage = [self getImageWithUnsaturatedPixelsOfImage:yourImage]; 
yourImage = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7]; 

您可以將色調的顏色更改爲任何您想要的顏色。

8

其最新和最簡單的方法來做到這一點。

  theImageView.image = [theImageView.image  imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
      [theImageView setTintColor:[UIColor redColor]]; 
相關問題