使用 Core Graphics 绘制正方形或包含 png?

发布于 2024-11-26 10:44:05 字数 193 浏览 1 评论 0原文

我正在为图表制作一个图例,基本上如下所示:

[ ] Line 1
[ ] Line 2
[ ] Line 3

左侧的框需要与图表上的线条颜色相同。

无论如何,我需要知道的是,使用 Core Graphics 绘制方框是否更快,或者只是使用 GIMP 为正方形制作一些 png 并包含它们。

I'm making a legend for a graph that will basically look like this:

[ ] Line 1
[ ] Line 2
[ ] Line 3

The boxes on the left need to be the same color as the lines on the graph.

Anyhow, all I need to know, is whether it's faster to draw the boxes with Core Graphics or just make some pngs with GIMP for the squares and include them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万劫不复 2024-12-03 10:44:05

对每个图例使用 UIView 并将其背景颜色设置为您想要的颜色。

Use UIView for each legend and set their background color to the color you want.

你没皮卡萌 2024-12-03 10:44:05

这两种方法都足够快,不会产生任何影响。然而,使用 Core Graphics 的优点是您可以更加灵活,例如当您稍后决定需要其他颜色时。另外,您的应用程序将会更小,因为您不必包含 PNG 文件。

Both approaches are fast enough that it shouldn't make a difference. However, using Core Graphics has the advantage that you're a lot more flexible, e.g. when you later decide that you need additional colors. Plus, your app will be smaller, because you don't have to include the PNG files.

旧人 2024-12-03 10:44:05

画盒子简直就是小菜一碟!我每天都会使用 Core Graphics,特别是因为你可以免费获得视网膜支持。

正如在此示例中所示,您可以仅使用 UIKit 类来完成此操作:

// Setup colors
[myBoxColor setFill];
[myBoxBorderColor set];
// Setup a path for the box
UIBezierPath* path = [UIBezierBath bezierPathWithRect:rectOfTheBox];
path.lineWidth = 2;
// Draw!
[path fill];
[path stroke];

一个警告;使用路径的边缘作为线条的中心进行描边填充。因此,如果您用线宽为 1 点的积分矩形绘制路径,您会得到一条模糊的线。

如果您想要一条 1 点线作为边界,可以通过执行以下操作来解决此问题:

CGRect strokeRect = UIEdgeInsetsInsetRect(rectOfTheBox, 
                                          UIEdgeInsetsMake(0.5f,0.5f,0.5f,0.5f));
UIBezierPath* path = [UIBezierPath bezierPathWithRect:strokeRect];
[path stroke];

Drawing boxes is a snap! I would go with Core Graphics everyday, especially since you get retina support for free.

As can be seen in this example you can do it using UIKit only classes:

// Setup colors
[myBoxColor setFill];
[myBoxBorderColor set];
// Setup a path for the box
UIBezierPath* path = [UIBezierBath bezierPathWithRect:rectOfTheBox];
path.lineWidth = 2;
// Draw!
[path fill];
[path stroke];

One warning; stroke fills using the edges of the path as the center of the line. So you will get a blurry line if you stroke a path with integral rect with a 1 point line width.

You can remedy this is you want a 1 point line for the border by doing something like this:

CGRect strokeRect = UIEdgeInsetsInsetRect(rectOfTheBox, 
                                          UIEdgeInsetsMake(0.5f,0.5f,0.5f,0.5f));
UIBezierPath* path = [UIBezierPath bezierPathWithRect:strokeRect];
[path stroke];
赠佳期 2024-12-03 10:44:05

在 iOS 上,Core Graphics 使用起来非常简单。在视图的 drawRect: 方法中,只需执行以下操作即可绘制一个正方形:

- (void)drawRect:(CGRect)frame {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1); // gray
    CGContextFillRect(context, CGRectMake(10, 10, 20, 20)); // our rect is {10,10,20,20)
    // draw a line
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, startX, startY);
    CGContextAddLineToPoint(context, endX, endY);
    CGContextStrokePath(context);
}

希望这会有所帮助!

On iOS, Core Graphics is quite simple to use. In your view's drawRect: method, just do this to draw a square:

- (void)drawRect:(CGRect)frame {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1); // gray
    CGContextFillRect(context, CGRectMake(10, 10, 20, 20)); // our rect is {10,10,20,20)
    // draw a line
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, startX, startY);
    CGContextAddLineToPoint(context, endX, endY);
    CGContextStrokePath(context);
}

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文