Is this code drawing at the point or pixel level? How to draw retina pixels?

前端 未结 3 1126
北海茫月
北海茫月 2020-12-03 18:56

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

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



        
相关标签:
3条回答
  • 2020-12-03 19:38

    [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]

    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.

    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).

    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().

    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.

    0 讨论(0)
  • 2020-12-03 19:39

    What it sounds like you are looking for is the scale property on UIScreen:

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

    This allows you to control the number of pixels the coordinate system gives you per virtual pixel. iOS devices basically work at non retina coordinates. Old link explaining what is going on here:

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

    Don't use his macros, as some devices are now scale of 3.0, but the post explains what is going on.

    0 讨论(0)
  • 2020-12-03 19:42

    So, based on the magnificent answer of KenThomases, and a day of testing, here's exactly how you draw at physical pixel level. I think.

    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
    }
    

    The critical elements:

    first ...

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

    second ...

        override open func draw(in ctx: CGContext) {
    
            realPixelSize = ctx.convertToDeviceSpace(bounds).size
            ...
        }
    

    third ...

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

    Example ...

    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
    

    It's absolutely critical to set contentsScale at initialization time.

    I tried some os versions, and it seems for better or worse the default for layers for contentsScale is unfortunately "1" rather than screen density, so, do not forget to set it!!! (Note that other systems in the OS will use it, also, to know how to handle your layer efficiently, etc.)

    0 讨论(0)
提交回复
热议问题