Setting A CGContext Transparent Background

后端 未结 8 763
长发绾君心
长发绾君心 2020-12-08 09:15

I am still struggling with drawing a line with CGContext. I have actually go to line to draw, but now I need the background of the Rect to be transparent so the existing ba

8条回答
  •  误落风尘
    2020-12-08 09:43

    This is what worked for me with a UIImage which had been manually added using InterfaceBuilder.

    - (id)initWithCoder:(NSCoder *)aDecoder {
    
        if(self = [super initWithCoder:aDecoder]) {
            self.backgroundColor = [UIColor clearColor];
        }
    
        return self;
    }
    
    
    -(void)drawRect:(CGRect)rect
    {
        CGContextRef context = UIGraphicsGetCurrentContext();    
        CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextSetLineWidth(context, 5.0);
        CGContextMoveToPoint(context, 100.0,0.0);
        CGContextAddLineToPoint(context,100.0, 100.0);
        CGContextStrokePath(context);
    }
    

    David Kanarek's answer only works when you're manually creating your own UIImageView. If you've created a UIView and manually added it via Interface Builder then you will need a different approach like this calling the initWithCoder method instead.

提交回复
热议问题