5

我试过了drawInRectCGContextDrawImage但它适用于整个图像。

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

4

4 回答 4

20

您可以将UIImageAlpha 通道用作绘图的遮罩。

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];

斯威夫特 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;
}
于 2013-05-24T07:37:46.740 回答
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;
于 2013-05-24T08:04:45.310 回答
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();
于 2013-05-24T15:50:59.430 回答
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]];
于 2013-07-05T21:02:17.153 回答