Search code examples
swiftuiimagecore-graphicscgimageargb

Turning raw ARGB pixeldata into UIImage


I want to be able to set all the pixels on the screen to a specific color. The method I am using is creating all the pixels in a raw data array and then create an UIImage which I put in an UIImageView. I cropped the code down to this which is still showing the problem, I think I have done something stupid by creating the image but I have read all the documentation and imho everything seems fine:

Struct for saving pixeldata

public struct PixelData {
    var a: UInt8 = 255
    var r: UInt8
    var g: UInt8
    var b: UInt8
}

Filling array left half red, right half black

func halfRood()->[PixelData] {
        var data: [PixelData] = []

        for(var i = 0; i < Int(Constants.Resolution.x); i++) {
            for(var j = 0; j < Int(Constants.Resolution.y); j++) {
                if(j < Int(Constants.Resolution.y/2)) {
                    data.append(PixelData(a: 255, r: 255, g: 0, b: 0))
                } else {
                    data.append(PixelData(a: 255, r: 0, g: 0, b: 0))
                }

            }
        }

        return data
    }

Return image from bitmap

func imageFromARGB32Bitmap(pixels: [PixelData], width: UInt, height: UInt) -> UIImage? {
    let bitsPerComponent: UInt = 8
    let bitsPerPixel: UInt = 32
    let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo:CGBitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue)

    var data = pixels
    let providerRef = CGDataProviderCreateWithCFData(NSData(bytes: &data, length: data.count * sizeof(PixelData)))
    let providerRefthing: CGDataProvider = providerRef
    let cgImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, width * UInt(sizeof(PixelData)), rgbColorSpace, bitmapInfo, providerRef, nil, true, kCGRenderingIntentDefault)
    let cgiimagething: CGImage = cgImage
    return UIImage(CGImage: cgImage)
}

And finally setting the updateUI() function

func updateUI() {
        let data = halfRood()
        if let image = imageFromARGB32Bitmap(data, width: UInt(Constants.Resolution.x), height: UInt(Constants.Resolution.y)) {
            imageView?.image = image
        }
    }

The output looks like the following picture, while I expected something like: [red half | black half]

currentSituation


Solution

  • CoreGraphics expects pixel data as rows, not columns. Just flip your for-statements like this:

    func halfRood()->[PixelData] {
        var data: [PixelData] = []
        for(var y = 0; y < Int(Constants.Resolution.y); y++) {
            for(var x = 0; x < Int(Constants.Resolution.x); x++) {
                if(y < Int(Constants.Resolution.y/2)) {
                    data.append(PixelData(a: 255, r: 255, g: 0, b: 0))
                } else {
                    data.append(PixelData(a: 255, r: 0, g: 0, b: 0))
                }
            }
        }
    
        return data
    }