开发者

drawing lines on top of UIImageView which has an image already

开发者 https://www.devze.com 2023-04-08 16:39 出处:网络
I have a picture black and white of png format that is displayed with an UIImageView. It is a clock shape with no handles. so the background is white - and the circle is black.

I have a picture black and white of png format that is displayed with an UIImageView. It is a clock shape with no handles. so the background is white - and the circle is black. Then I would like to draw on top the handles for hours and minutes and I am struggling on how to do that although I am not far I can't make it to work...

When I draw the handles I end up with a big black square (so I can't see through it to see my png behind it) and at least I see my 2 handles (in blue which I wanted). I guess the goal is to make that big black square to be white/transparent and so it should work...

My code is like that below:

- (void)drawRect:(CGRect)rect 
{
......

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);    
[self drawLineForContext:context Width:10.0 angle:hAlpha length:self.frame.size.width/2.0 - 40];
[self drawLineForContext:context Width:5.0 angle:mAlpha length:self.frame.size.width/2.0 - 12];
}

What do you think I am missing?

For information, to set the image, I just do it like that, I guess it is correct as it works...

UIImage *image = [UIImage imageNamed: [md objectForKey:Q_DIRECTION_PIC]];
[ivDirectionPic setImage: image];
ivDirectionPic.backgroundColor=[UIColor clearColor];
ivDirectionPic.contentMode = UIViewContentModeScaleAspectFit;

Thanks for your help!

Cheers, geebee

开发者_运维百科

EDIT1: I added the UIColor clearColor but it is still the same... and I do not understand your comment on how to add another subclass with same frame: how please? where?

I am added also the extra function you asked me to post:

- (void) drawLineForContext:(const CGContextRef)context Width:(float)_width angle:(double)_angle length:(double)radius
{
CGPoint c = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);

CGContextSetLineWidth(context, _width);
CGContextMoveToPoint(context, self.center.x, self.center.y);
CGPoint addLines[] =
{
    CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0),
    CGPointMake(radius*cos(_angle) +c.x, radius*sin(_angle) +c.y),
};

CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
CGContextStrokePath(context);
}


Your UIImageView needs to have its backgroundColor set to [UIColor clearColor].

If this does not work, simply add add another subclass of UIView with the same frame on top of your image view.

Another source of your error might be your drawLineForContext methods for which you did not post any code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号