此代码是在点还是像素级别绘制的?如何绘制视网膜像素?

时间:2017-12-02 16:57:19

标签: ios core-graphics metal

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

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

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

然后

angleGradient(data, w, h ..

并且它遍历所有那些

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

基本上设置颜色

    *p++ = color;

但是等等 - 这不是以点数而非像素来运作吗?

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

是一个问题:

  

我们说设备上的密度为4。像上面的代码一样绘制,但是,在位图上大四倍,然后把它放在矩形中?

这看起来很乱 - 但就是这样吗?

3 个答案:

答案 0 :(得分:5)

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

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

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

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

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

答案 1 :(得分:1)

因此,基于KenThomases的精彩答案以及一天的测试,这就是您在物理像素级别的绘制方式。我想。

class PixelwiseLayer: CALayer {

    override init() {

        super.init()
        // SET THE CONTENT SCALE AT INITIALIZATION TIME
        contentsScale = UIScreen.main.scale
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override open func draw(in ctx: CGContext) {

        let rectDEVICESPACE = ctx.convertToDeviceSpace(bounds).size
        // convertToDeviceSpace >>KNOWS ABOUT CONTENT SCALE<<
        // and YOU have CORRECTLY SET content scale at initialization time

        // write pixels to DEVICE SPACE, BUT ...
        let img = pixelByPixelImage(sizeInDeviceSpace: rectDEVICESPACE)

        // ... BUT the draw# call uses only the NORMAL BOUNDS
        ctx.draw(img, in: bounds)
    }

    private func pixelByPixelImage(sizeInDeviceSpace: CGSize) -> CGImage {

        let wPIXELS = Int(sizeInDeviceSpace.width)
        let hPIXELS = Int(sizeInDeviceSpace.height)
        // !!!THAT IS ACTUAL PIXELS!!!

        // you !!!DO NOT!!! need to multiply by UIScreen.main.scale,
        // as is seen in much example code.
        // convertToDeviceSpace does it properly.

        let bitsPerComponent: Int = MemoryLayout<UInt8>.size * 8
        let bytesPerPixel: Int = bitsPerComponent * 4 / 8
        let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

        var data = [RGBA]()

        for y in 0..<hPIXELS {
            for x in 0..<wPIXELS {

                let c = yourPixelColor(atPoint: x .. y)
                data.append(c)
            }
        }

        // the context ... use actual pixels!!!!
        let ctx = CGContext(data: &data,
                    width: wPIXELS, height: hPIXELS,
                    bitsPerComponent: bitsPerComponent,
                    bytesPerRow: wPIXELS * bytesPerPixel,
                    space: colorSpace,
                    bitmapInfo: bitmapInfo.rawValue)
        let img = ctx?.makeImage()!
        return img!  // return a CGImage in actual pixels!!!!!!
    }

    // (PS, it's very likely you'll want needsDisplayOnBoundsChange as with most layers.
    // Set it true in init(), needsDisplayOnBoundsChange = true )

}

fileprivate struct RGBA { // (build raw data like this)
    var r: UInt8
    var g: UInt8
    var b: UInt8
    var a: UInt8
}

关键要素:

首先......

        super.init()
        // SET THE CONTENT SCALE >>>>AT INITIALIZATION TIME<<<<
        contentsScale = UIScreen.main.scale

第二......

    override open func draw(in ctx: CGContext) {

        realPixelSize = ctx.convertToDeviceSpace(bounds).size
        ...
    }

第三......

    override open func draw(in ctx: CGContext) {

        ...
        your image = yourPixelDrawingFunction( realPixelSize ) // NOT BOUNDS
        ctx.draw(img, in: bounds)  // NOT REALPIXELSIZE
    }

示例......

console:
contentsScale 3.0
UIScreen.main.scale 3.0
bounds (0.0, 0.0, 84.0, 84.0)
rectDEVICESPACE (252.0, 252.0)
actual pixels being created as data: w, h 252, 252

在初始化时设置contentsScale绝对至关重要。

我尝试了一些操作系统版本,似乎无论好坏,contentsScale的图层默认值是“1”而不是屏幕密度,所以,不要忘记设置它! (请注意,操作系统中的其他系统也会使用它来了解如何有效地处理图层等。)

答案 2 :(得分:0)

您正在寻找的是UIScreen上的比例属性:

https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale

这允许您控制坐标系为每个虚拟像素提供的像素数。 iOS设备基本上可以在非视网膜坐标上工作。旧链接解释这里发生了什么:

http://www.daveoncode.com/2011/10/22/right-uiimage-and-cgimage-pixel-size-retina-display/

不要使用他的宏,因为有些设备现在的规模为3.0,但帖子解释了发生了什么。