0

我有一个 UIView 子类,我想用它来绘制具有不同混合模式的图像。

代码:

@implementation CompositeImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)setBlendMode:(CGBlendMode) composite
{
    blender = composite;
}

-(void)setImage:(UIImage*) img
{
    image = img;
}

- (void)drawRect:(CGRect)rect
{
    NSLog(@"it draws");
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(c, blender);
    CGContextDrawImage(c, rect, image.CGImage);
}

@end

我用来设置它的代码是:

[testImage setImage:[UIImage imageNamed:@"Prayer_Background_Paid"]];
[testImage setBlendMode:kCGBlendModeColor];
[testImage setNeedsDisplay];

我正在使用界面生成器放置一个大矩形UIView,然后将其类设置为CompositeImageView。但是,它仍然绘制为一个大的白色正方形。即使我注释掉drawRect中的所有内容,它仍然会绘制白色方块。但是,它正在调用 drawRect,因为正在记录“它绘制”。

4

1 回答 1

0

你确定 kCGBlendModeColor 是你想要的吗?来自苹果文档:

将背景的亮度值与源图像的色调和饱和度值结合使用。

似乎如果它是白色背景,混合图像也会显示为白色。

于 2012-04-28T01:54:13.583 回答