2012-04-28 53 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