在UIImageView上绘制线条已经有一个图像

时间:2011-09-24 14:45:23

标签: iphone uiimageview line draw cgcontext

我有一张用UIImageView显示的png格式的黑白图片。这是一个没有把手的钟形。所以背景是白色的 - 圆圈是黑色的。 然后我想画几小时和几分钟的手柄,我正在努力做到这一点,虽然我不远,我不能让它工作......

当我画出手柄时,我最终得到了一个大的黑色方块(所以我无法通过它看到我背后的png),至少我看到了我的2个手柄(蓝色是我想要的)。我想我的目标是让那个大的黑色方块变成白色/透明,所以它应该有效......

我的代码如下:

- (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];
}

你认为我错过了什么?

有关信息,要设置图像,我只是这样做,我想这是正确的,因为它有效...

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

感谢您的帮助!

干杯, geebee

EDIT1:我添加了UIColor clearColor但它仍然是相同的......我不明白你对如何添加另一个具有相同框架的子类的评论:请问怎么样?在哪里?

我还添加了您要求我发布的额外功能:

- (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);
}

1 个答案:

答案 0 :(得分:0)

您的UIImageView需要将backgroundColor设置为[UIColor clearColor]

如果这不起作用,只需在图像视图顶部添加另一个UIView的子类,并使用相同的框架。

您的错误的另一个来源可能是您未发布任何代码的drawLineForContext方法。

相关问题