2012-04-28 51 views
0

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

代码:

@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,因为“正在绘制”正在被记录。

+0

你确定你的图片不是'nil'(可能是文件名中的拼写错误)吗? – omz 2012-04-28 03:30:19

+0

是的。我还尝试了一些项目中的其他图像。除非我得到/绘制图像的方式有什么问题,否则我不知道什么可能是错的 - 因此我在这里发布。 (不知道什么与反对票) – Nyth 2012-05-07 16:20:26

+0

你能够使这项工作?我现在正在尝试做同样的事情。 – 2014-07-27 11:31:53

回答

0

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

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

看来如果是白色背景,混合图像也会显示为白色。

+0

颜色只是我尝试过的其中之一 - 正如我所提到的,我试图完全注释掉drawRect方法,并且仍然出现白色矩形。 – Nyth 2012-04-28 02:12:33

+0

尝试设置self.backgroundColor = [UIColor clearColor];在init中。 – 2012-04-28 02:19:20

+0

设置背景颜色清除,使其根本不绘制,虽然它仍然调用drawRect方法... – Nyth 2012-04-28 04:57:20