【发布时间】:2011-09-24 14:45:22
【问题描述】:
我有一张用 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;
感谢您的帮助!
干杯, 啧啧
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);
}
【问题讨论】:
标签: iphone uiimageview line draw cgcontext