ios 如何使用 Core Graphics 绘制点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/313018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:24:42  来源:igfitidea点击:

How do I draw a point using Core Graphics?

ioscocoa-touchcore-graphics

提问by

I see APIs in Quartz for drawing lines and circles. But all I want to do is to specify the (x,y) cartesian coordinate to color a pixel a particular value. How do I do that?

我在 Quartz 中看到了用于绘制线条和圆形的 API。但我想要做的就是指定 (x,y) 笛卡尔坐标以将像素着色为特定值。我怎么做?

回答by

CGContextFillRect(context, CGRectMake(x,y,1,1));

回答by Jens Ayton

Quartz is not a pixel-oriented API, and its contexts aren't necessarily pixel buffers. If you want to draw pixmaps, create a bitmap context with CGBitmapContextCreate(). You provide a buffer, which you can manipulate directly, and can copy to another context by creating a CGImagefrom the same buffer using CGImageCreate()and drawing that.

Quartz 不是面向像素的 API,它的上下文不一定是像素缓冲区。如果要绘制像素图,请使用CGBitmapContextCreate(). 你提供了一个缓冲区,你可以直接操作它,并且可以通过CGImage使用CGImageCreate()和绘制它从同一个缓冲区创建一个来复制到另一个上下文。

回答by Paulo

Draw a very small ellipse/circle and fill it!

画一个非常小的椭圆/圆并填充它!

CGContextAddEllipseInRect(Context,(CGRectMake (x_dot, y_dot, 3.0, 3.0));
CGContextDrawPath(Context, kCGPathFill);
CGContextStrokePath(Context);

I am using this code to create a small dot (3x3 pixels) in a dotted music note.

我正在使用此代码在带点音符的音符中创建一个小点(3x3 像素)。

回答by Chad

I got a point (zero length line) to draw after setting the line-caps to kCGLineCapRound. The default line-cap has no length so can't be drawn.

将线帽设置为 kCGLineCapRound 后,我得到了一个要绘制的点(零长度线)。默认线帽没有长度,因此无法绘制。

The argument that a point has no size is silly. The lines have no width but we can draw those (by using the "line width" from the draw state). A point should draw in exactly the same way, and with different line caps I believe it does.

点没有大小的论点是愚蠢的。线条没有宽度,但我们可以绘制它们(通过使用绘制状态中的“线条宽度”)。一个点应该以完全相同的方式绘制,并且我相信它可以使用不同的线帽。

Maybe this behavior is new?

也许这种行为是新的?

回答by Ben Gottlieb

You can draw a 1-pixel-length line at the coordinate in question; that should accomplish what you want.

您可以在相关坐标处绘制一条 1 像素长的线;这应该完成你想要的。

回答by Ben Gottlieb

I'm having the same issue - i find the best solution is similar to the last, but at least it doesn't leave something that looks like a "dash"... of course, should ensure x/y are both > 0.

我遇到了同样的问题 - 我发现最好的解决方案与最后一个类似,但至少它不会留下看起来像“破折号”的东西......当然,应该确保 x/y 都 > 0 .

CGContextFillRect(context, CGRectMake(x - 0.5, y - 0.5, 1.0 , 1.0));

CGContextFillRect(context, CGRectMake(x - 0.5, y - 0.5, 1.0, 1.0));

回答by Antoine Weber

swift 3 :

快速3:

 UIRectFill(CGRect(x: x, y: y, width: 1, height: 1))