2013-10-02 24 views
2

如果我有一些透明像素的圖像,是否有可能用白色對透明像素進行着色,並使圖像的其餘部分在objective-c中透明?IOS:只對透明像素進行顏色

謝謝!

+0

所以它張貼作爲一個答案:) –

回答

1

我找到了一個解決方案:

- (UIImage*)convertToInverseWhiteMask: (UIImage *) image { 
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); 
    CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    // Draw a white background (for white mask) 
    CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f); 
    CGContextFillRect(ctx, imageRect); 

    // Apply the source image's alpha 
    [image drawInRect:imageRect blendMode:kCGBlendModeDestinationOut alpha:1.0f]; 

    UIImage* mask = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return mask; 
    } 
0

這裏是我使用的是什麼:

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

    UIGraphicsBeginImageContextWithOptions(size, FALSE, [[UIScreen mainScreen] scale]); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

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

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

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

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

    return tintedImage; 
}