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

ios – 如何将透明PNG图像与颜色合并,第1张

概述我尝试使用drawInRect和CGContextDraw Image,但它适用于整个图像. 我希望它仅适用于图像部分,而不是透明部分.有谁知道怎么做? 您可以使用带有alpha通道的UIImage作为绘图掩码. Objective-C的 - (UIImage *)overlayImage:(UIImage *)image withColor:(UIColor *)color{ // 我尝试使用drawInRect和CGContextDraw Image,但它适用于整个图像.

我希望它仅适用于图像部分,而不是透明部分.有谁知道怎么做?

@R_403_6120@ 您可以使用带有Alpha通道的UIImage作为绘图掩码.

Objective-C的

- (UIImage *)overlayImage:(UIImage *)image withcolor:(UIcolor *)color{    //  Create rect to fit the PNG image    CGRect rect = CGRectMake(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];

斯威夫特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,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图像与颜色合并全部内容,希望文章能够帮你解决ios – 如何将透明PNG图像与颜色合并所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081606.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存