如何着色图像/显示颜色?

时间:2011-02-28 18:23:44

标签: iphone objective-c xcode

我想做的2件事,这些事情是相关的:

  1. 显示任意颜色的块。所以我可以随时将这种颜色改成其他颜色。
  2. 将UIImage染成不同的颜色。阿尔法调低的颜色叠加可以在这里工作,但是说它是一个具有透明背景并且没有占据图像的整个正方形的图像。
  3. 有什么想法吗?

5 个答案:

答案 0 :(得分:11)

另一个选择是在UIImage上使用类别方法,就像这样......

// Tint the image, default to half transparency if given an opaque colour.
- (UIImage *)imageWithTint:(UIColor *)tintColor {
    CGFloat white, alpha;
    [tintColor getWhite:&white alpha:&alpha];
    return [self imageWithTint:tintColor alpha:(alpha == 1.0 ? 0.5f : alpha)];
}

// Tint the image
- (UIImage *)imageWithTint:(UIColor *)tintColor alpha:(CGFloat)alpha {

    // Begin drawing
    CGRect aRect = CGRectMake(0.f, 0.f, self.size.width, self.size.height);
    UIGraphicsBeginImageContext(aRect.size);

    // Get the graphic context
    CGContextRef c = UIGraphicsGetCurrentContext(); 

    // Converting a UIImage to a CGImage flips the image, 
    // so apply a upside-down translation
    CGContextTranslateCTM(c, 0, self.size.height); 
    CGContextScaleCTM(c, 1.0, -1.0);

    // Draw the image
    [self drawInRect:aRect];

    // Set the fill color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextSetFillColorSpace(c, colorSpace);

    // Set the mask to only tint non-transparent pixels
    CGContextClipToMask(c, aRect, self.CGImage);

    // Set the fill color
    CGContextSetFillColorWithColor(c, [tintColor colorWithAlphaComponent:alpha].CGColor);

    UIRectFillUsingBlendMode(aRect, kCGBlendModeColor);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    // Release memory
    CGColorSpaceRelease(colorSpace);

    return img;
}

答案 1 :(得分:10)

第一个很容易。制作一个新的UIView并将其背景颜色设置为您想要的任何颜色。

第二个更难。正如你所提到的,你可以在透明度调低的情况下在其上面放置一个新视图,但要让它在相同位置剪辑,你需要使用一个遮罩。像这样:

UIImage *myImage = [UIImage imageNamed:@"foo.png"];

UIImageView *originalImageView = [[UIImageView alloc] initWithImage:myImage];
[originalImageView setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[parentView addSubview:originalImageView];

UIView *overlay = [[UIView alloc] initWithFrame:[originalImageView frame]];

UIImageView *maskImageView = [[UIImageView alloc] initWithImage:myImage];
[maskImageView setFrame:[overlay bounds]];

[[overlay layer] setMask:[maskImageView layer]];

[overlay setBackgroundColor:[UIColor redColor]];

[parentView addSubview:overlay];

请记住,您必须在实施文件中#import <QuartzCore/QuartzCore.h>

答案 2 :(得分:4)

这是实现图像着色的另一种方法,特别是如果你已经将QuartzCore用于其他方面。

导入QuartzCore:

#import <QuartzCore/QuartzCore.h>    

创建透明CALayer并将其添加为您想要着色的图像的子图层:

CALayer *sublayer = [CALayer layer];
[sublayer setBackgroundColor:[UIColor whiteColor].CGColor];
[sublayer setOpacity:0.3];
[sublayer setFrame:toBeTintedImage.frame];
[toBeTintedImage.layer addSublayer:sublayer];

将QuartzCore添加到您的项目框架列表中(如果它尚未存在),否则您将收到如下编译器错误:

Undefined symbols for architecture i386: "_OBJC_CLASS_$_CALayer"

答案 3 :(得分:2)

实现1的简单方法是创建UILabel甚至是UIView并根据需要更改backgroundColor。

有一种方法可以将颜色相乘而不是仅覆盖颜色,这应该适用于2.请参阅this tutorial

答案 4 :(得分:-1)

试试这个

   - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIView* maskedView = [self filledViewForPNG:[UIImage imageNamed:@"mask_effect.png"]
                                               mask:[UIImage imageNamed:@"mask_image.png"]
                                          maskColor:[UIColor colorWithRed:.6 green:.2 blue:.7 alpha:1]];


        [self.view addSubview:maskedView];

    }

    -(UIView*)filledViewForPNG:(UIImage*)image mask:(UIImage*)maskImage maskColor:(UIColor*)maskColor
    {
        UIImageView *pngImageView = [[UIImageView alloc] initWithImage:image];
        UIImageView *maskImageView = [[UIImageView alloc] initWithImage:maskImage];

        CGRect bounds;
        if (image) {
            bounds = pngImageView.bounds;
        }
        else
        {
            bounds = maskImageView.bounds;
        }
        UIView* parentView = [[UIView alloc]initWithFrame:bounds];
        [parentView setAutoresizesSubviews:YES];
        [parentView setClipsToBounds:YES];

        UIView *overlay = [[UIView alloc] initWithFrame:bounds];
        [[overlay layer] setMask:[maskImageView layer]];
        [overlay setBackgroundColor:maskColor];

        [parentView addSubview:overlay];
        [parentView addSubview:pngImageView];
        return parentView;
    }