此代码是在点还是像素级别绘制的?如何绘制视网膜像素? [英] Is this code drawing at the point or pixel level? How to draw retina pixels?

查看:121
本文介绍了此代码是在点还是像素级别绘制的?如何绘制视网膜像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个令人钦佩的脚本绘制(圆形)渐变,

Consider this admirable script which draws a (circular) gradient,

https://github.com/paiv/AngleGradientLayer/blob/master/AngleGradient/AngleGradientLayer.m

int w = CGRectGetWidth(rect);
int h = CGRectGetHeight(rect);

然后

angleGradient(data, w, h ..

并且它循环遍及所有这些

and the it loops over all those

for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {

基本上设置颜色

    *p++ = color;

但等待 - 这不是按点,而不是像素吗?

But wait - wouldn't this be working by points, not pixels?

你真的会在密集屏幕上绘制物理像素吗?

How, really, would you draw to the physical pixels on dense screens?

是否属于:


假设密度为4如上面的代码那样绘制,但是,在位图上大四倍,然后把它放在矩形中?

Let's say the density is 4 on the device. Draw just as in the above code, but, on a bitmap four times as big, and then put it in the rect?



<那看起来很乱 - 但是它呢?

That seems messy - but is that it?

推荐答案

[注意:github示例中的代码 not 计算像素b上的渐变asis。github示例中的代码以点为单位计算梯度。 -Fattie]

[Note: The code on the github example does not calculate the gradient on a pixel basis. The code on the github example calculates the gradient on a points basis. -Fattie]

代码以像素为单位。首先,它使用像素颜色数据填充简单的栅格位图缓冲区。这显然没有像素以外的图像比例或单位的概念。接下来,它从该缓冲区创建一个 CGImage (有点奇怪)。 CGImage 也没有像素以外的比例或单位的概念。

The code is working in pixels. First, it fills a simple raster bitmap buffer with the pixel color data. That obviously has no notion of an image scale or unit other than pixels. Next, it creates a CGImage from that buffer (in a bit of an odd way). CGImage also has no notion of a scale or unit other than pixels.

问题出现在哪里 CGImage 被绘制。是否在此时完成缩放取决于图形上下文及其配置方式。在上下文中有一个隐式转换,可以从用户空间(点或更多或更少)转换为设备空间(像素)。

The issue comes in where the CGImage is drawn. Whether scaling is done at that point depends on the graphics context and how it has been configured. There's an implicit transform in the context that converts from user space (points, more or less) to device space (pixels).

-drawInContext :方法应该使用 CGContextConvertRectToDeviceSpace()转换矩形以获取图像的矩形。请注意,未转换的rect仍应用于调用 CGContextDrawImage()

The -drawInContext: method ought to convert the rect using CGContextConvertRectToDeviceSpace() to get the rect for the image. Note that the unconverted rect should still be used for the call to CGContextDrawImage().

因此,对于2x Retina显示上下文,原来的rect将在点。比方说100x200。图像rect的大小将加倍以表示像素,200x400。绘制操作将绘制到100x200 rect,这可能看起来像是会缩小大的,高度详细的图像,丢失信息。但是,在内部,绘制操作会在执行实际绘制之前将目标rect缩放到设备空间,并从200x400像素图像填充200x400像素区域,保留所有细节。

So, for a 2x Retina display context, the original rect will be in points. Let's say 100x200. The image rect will be doubled in size to represent pixels, 200x400. The draw operation will draw that to the 100x200 rect, which might seem like it would scale the large, highly-detailed image down, losing information. However, internally, the draw operation will scale the target rect to device space before doing the actual draw, and fill a 200x400 pixel area from the 200x400 pixel image, preserving all of the detail.

这篇关于此代码是在点还是像素级别绘制的?如何绘制视网膜像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆