我有一個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,因爲「正在繪製」正在被記錄。
你確定你的圖片不是'nil'(可能是文件名中的拼寫錯誤)嗎? – omz 2012-04-28 03:30:19
是的。我還嘗試了一些項目中的其他圖像。除非我得到/繪製圖像的方式有什麼問題,否則我不知道什麼可能是錯的 - 因此我在這裏發佈。 (不知道什麼與反對票) – Nyth 2012-05-07 16:20:26
你能夠使這項工作?我現在正在嘗試做同樣的事情。 – 2014-07-27 11:31:53