gpt4 book ai didi

ios - 如何将透明PNG图像与颜色合并

转载 作者:可可西里 更新时间:2023-11-01 05:01:32 26 4
gpt4 key购买 nike

我尝试使用 drawInRectCGContextDrawImage 但它应用于整个图像。

我希望它只应用于图像部分,而不是透明部分。有谁知道怎么做吗?

最佳答案

您可以使用带 alpha channel 的 UIImage 作为绘图蒙版。

objective-C

- (UIImage *)overlayImage:(UIImage *)image withColor:(UIColor *)color
{
// Create rect to fit the PNG image
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);

// Start drawing
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

// Fill the rect by the final color
[color setFill];
CGContextFillRect(context, rect);

// Make the final shape by masking the drawn color with the images alpha values
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
[image drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1];

// Create new image from the context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

// Release context
UIGraphicsEndImageContext();

return img;
}

示例用法:

UIImage *pngImage = [UIImage imageNamed:@"myImage"];
UIColor *overlayColor = [UIColor magentaColor];

UIImage *image = [self overlayImage:pngImage withColor:overlayColor];

swift 3

extension UIImage {

func overlayed(by overlayColor: UIColor) -> UIImage {
// Create rect to fit the image
let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)

// Create image context. 0 means scale of device's main screen
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let context = UIGraphicsGetCurrentContext()!

// Fill the rect by the final color
overlayColor.setFill()
context.fill(rect)

// Make the final shape by masking the drawn color with the images alpha values
self.draw(in: rect, blendMode: .destinationIn, alpha: 1)

// Create new image from the context
let overlayedImage = UIGraphicsGetImageFromCurrentImageContext()!

// Release context
UIGraphicsEndImageContext()

return overlayedImage
}
}

示例用法:

let overlayedImage = myImage.overlayed(by: .magenta)

编辑:正如 Desdenova 在评论中指出的那样,我误解了这个问题。我最初的回答是在彩色背景上绘制 PNG。答案已编辑,以下代码为原始代码。

- (UIImage *)combineImage:(UIImage *)image withBackgroundColor:(UIColor *)bgColor
{
// Create rect to fit the PNG image
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);

// Create bitmap contect
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);

// Draw background first

// Set background color (will be under the PNG)
[bgColor setFill];

// Fill all context with background image
CGContextFillRect(context, rect);

// Draw the PNG over the background
[image drawInRect:rect];

// Create new image from the context
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

// Release context
UIGraphicsEndImageContext();

return img;
}

关于ios - 如何将透明PNG图像与颜色合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16729628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com