【发布时间】:2013-10-08 23:40:59
【问题描述】:
如果我有一个带有一些透明像素的图像,是否有可能用白色为透明像素着色并在objective-c 中使图像的其余部分透明?
谢谢!
【问题讨论】:
-
所以把它作为答案发布:)
标签: ios objective-c colors transparent mask
如果我有一个带有一些透明像素的图像,是否有可能用白色为透明像素着色并在objective-c 中使图像的其余部分透明?
谢谢!
【问题讨论】:
标签: ios objective-c colors transparent mask
这是我正在使用的:
- (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;
}
【讨论】:
我找到了解决办法:
- (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;
}
【讨论】: