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

时间:2013-05-24 07:20:20

标签: ios objective-c ipad

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

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

4 个答案:

答案 0 :(得分:18)

您可以将UIImage与Alpha通道一起用作绘图的掩码。

目标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;
}

答案 1 :(得分:0)

我在点击按钮时应用了以下代码,用透明的PNG填充颜色。

UIImage *colouredImage = _oldImageView.image;
colouredImage = [colouredImage imageTintedWithColor:[UIColor colorWithRed:99/255.0 green:4/255.0 blue:96/255.0 alpha:1.0]];
_oldImageView.image = colouredImage;

答案 2 :(得分:0)

感谢您的帮助...我尝试了这个代码,它最终为我工作。

UIGraphicsBeginImageContext(firstImage.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, firstImage.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGRect rect = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height);
// draw black background to preserve color of transparent pixels
CGContextSetBlendMode(context, kCGBlendModeNormal);
[[UIColor whiteColor] setFill];
CGContextFillRect(context, rect);
// draw original image
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextDrawImage(context, rect, firstImage.CGImage);
// tint image (loosing alpha) - the luminosity of the original image is preserved
CGContextSetBlendMode(context, kCGBlendModeDarken);
[[UIColor colorWithPatternImage:secondImage] setFill];
CGContextFillRect(context, rect);
// mask by alpha values of original image
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
CGContextDrawImage(context, rect, firstImage.CGImage);
//[firstImage drawInRect:rect];

// image drawing code here
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 3 :(得分:0)

我也遇到过这个问题,我认为@Lukas Kukacka答案是一个很好的解决方案。 我想为此解决方案添加更多内容,我建议使用类别。 为何分类?因为在我的情况下,我在多个类中使用这个“方法”,所以不是多次编写相同的代码,而是只做一次。

让我们看看代码: 这将是UIImage + OverlayTintColor.h

#import <UIKit/UIKit.h>

@interface UIImage (OverlayTintColor)
- (UIImage *)overlayTintColor:(UIColor *)tintColor;

@end

这将是UIImage + OverlayTintColor.m

#import "UIImage+OverlayTintColor.h"

@implementation UIImage (OverlayTintColor)

- (UIImage *)overlayTintColor:(UIColor *)tintColor{

        //  Create rect to fit the PNG image
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

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

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

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

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

        //  Release context
        UIGraphicsEndImageContext();

        return img;
}

@end

用法:(别忘了在你要使用它的类的顶部导入类别)

#import "UIImage+OverlayTintColor.h"


.......



 UIImage *image=[[UIImage imageNamed:@"test.png"] overlayTintColor:[UIColor blueColor]];