Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将透明PNG图像与彩色图像合并_Ios_Objective C_Ipad - Fatal编程技术网

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

Ios 如何将透明PNG图像与彩色图像合并,ios,objective-c,ipad,Ios,Objective C,Ipad,我尝试了drawInRect和CGContextDrawImage,但它适用于整个图像 我希望它只适用于图像部分,而不是透明部分。有人知道怎么做吗?你可以使用UIImage和alpha通道作为绘图的遮罩 目标-C 用法示例: UIImage *pngImage = [UIImage imageNamed:@"myImage"]; UIColor *overlayColor = [UIColor magentaColor]; UIImage *image = [self overlayImage

我尝试了
drawInRect
CGContextDrawImage
,但它适用于整个图像


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

你可以使用
UIImage
和alpha通道作为绘图的遮罩

目标-C 用法示例:

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

UIImage *image = [self overlayImage:pngImage withColor:overlayColor];
let overlayedImage = myImage.overlayed(by: .magenta)
斯威夫特3 用法示例:

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

UIImage *image = [self overlayImage:pngImage withColor:overlayColor];
let overlayedImage = myImage.overlayed(by: .magenta)

编辑:正如德斯德诺娃在评论中指出的那样,我误解了这个问题。我最初的答案是在彩色背景上绘制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;
}

我在点击按钮时应用了下面的代码,用颜色填充我的透明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;

谢谢你的帮助…我试过这个代码,它终于对我起作用了

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();

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

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

#import <UIKit/UIKit.h>

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

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


如果有人想努力回答你的问题,你可能想努力写下你的问题。。。。1) 展示你的代码2)你期望什么?3) 你得到了什么?
给一个人一条鱼,你喂他一天;告诉他如何捕鱼,你就可以喂他一辈子
我想他需要的是另一种方式。保持透明,其余的都涂上油漆。@Bala“给一个人生火,他会温暖一天;给他生火,他会温暖一辈子。”@Desdnova你是对的,我误解了这个问题。答案是肯定的,thanks@Maarten我想温暖你一辈子;)
#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]];