简体   繁体   English

UIView子类中的drawRect不会更新图像

[英]drawRect in UIView subclass doesn't update image

I have a UIView subclass that I would like to use to draw images with different blend modes. 我有一个UIView子类,我想用它来绘制具有不同混合模式的图像。

code: 码:

@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

the code that I use to set it up it is: 我用来设置它的代码是:

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

I am using the interface builder to place a large rectangular UIView, and then set its class to CompositeImageView. 我正在使用界面生成器放置一个大的矩形UIView,然后将其类设置为CompositeImageView。 However, it still draws as a large white square. 但是,它仍然绘制为一个大的白色正方形。 Even if I comment out everything inside drawRect, it still draws the white square. 即使我注释了drawRect内的所有内容,它仍然会绘制白色正方形。 However, it IS calling drawRect, because "it draws" is being logged. 但是,它正在调用drawRect,因为正在记录“绘制”。

Are you sure kCGBlendModeColor is what you want? 确定要使用kCGBlendModeColor吗? From the Apple doc: 从Apple文档中:

Uses the luminance values of the background with the hue and saturation values of the source image. 将背景的亮度值与源图像的色相和饱和度值一起使用。

It seems that if it was a white background, the blended image would also appear white. 看起来如果是白色背景,则混合后的图像也会显示为白色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM