[英]Drawing a circle in iOS
Trying to draw circles (well pinpoints as stars) onto a black background. 试图在黑色背景上绘制圆圈(精确定位为星星)。 Searched through Stack but none of the other solutions are working.
通过堆栈搜索,但其他解决方案均无效。
Trying to use; 尝试使用;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 5, 5));
CGContextSetRGBFillColor(contextRef, 0, 0, 0, 1.0);
The circles are not appearing and the following error message is appearing in the log: 没有出现圆圈,并且日志中出现以下错误消息:
<Error>: CGContextSetRGBFillColor: invalid context 0x0.
<错误>:CGContextSetRGBFillColor:无效的上下文0x0。 This is a serious error.
这是一个严重的错误。 This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability.
此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性整体下降。 This notice is a courtesy: please fix this problem.
此通知是礼貌的:请解决此问题。 It will become a fatal error in an upcoming update.
在即将进行的更新中,它将成为致命错误。
Any help to solve this (even completely different/easier way to achieve my goal) would be greatly appreciated. 任何解决此问题的帮助(甚至完全不同/更容易实现我的目标的方法)将不胜感激。
I used this in a recent project and worked fine: 我在最近的项目中使用了此工具,效果很好:
-(UIView *)circleWithColor:(UIColor *)color radius:(int)radius {
UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2 * radius, 2 * radius)];
circle.backgroundColor = color;
circle.layer.cornerRadius = radius;
circle.layer.masksToBounds = YES;
return circle;
}
and then add to current view (for example in a UIViewController subclass): 然后添加到当前视图(例如,在UIViewController子类中):
UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
[self.view addSubview:instoreImageDot];
try this to get a circle 试试这个圈
UIView * circle = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 10, 10)];
circle.layer.borderWidth = 2.0f;
circle.layer.borderColor = [UIColor redColor].CGColor;
circle.layer.cornerRadius = circle.frame.size.width/2.0;
circle.layer.masksToBounds = YES;
[self.view addSubview: circle];
If you are just trying to draw a circle on screen using CAShapeLayer is the simplest method. 如果您只是想使用CAShapeLayer在屏幕上画一个圆是最简单的方法。
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)].CGPath;
circle.fillColor = [UIColor blueColor].CGColor;
[self.view.layer addSublayer:circle];
This code create a CAShapeLayer and then sets the path of it be an oval in a CGRect you define. 此代码创建一个CAShapeLayer,然后在您定义的CGRect中将其路径设置为椭圆形。 This one draws a 100x100 circle on the top left side of the screen.
这在屏幕的左上方绘制了一个100x100的圆圈。 Then you add the layer to any other layer.
然后将图层添加到其他任何图层。 So for a basic implementation just add it the the layer of the UIView you are working with.
因此,对于基本实现,只需将其添加到您正在使用的UIView层中即可。 Hope it helps.
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.