简体   繁体   English

图像颜色错误?

[英]Image color bug?

I'd like to generate this cool like on an UIImage using the code I've added below. 我想使用下面添加的代码在UIImage上生成这种酷酷的效果。 But there seems to be a crazy bug in relation to the color : 但是似乎有一个与颜色有关的疯狂错误

So I'd like my function to draw lines to the image with a given RGB value (in my case r:76, g:255, b:0 to get this green look) as it's color: 所以我希望我的函数以给定的RGB值(在我的情况下, r:76, g:255, b:0以获得绿色外观)在图像上绘制线条:

img1

But the code I'm using just colors the image kind of magenta instead of green : 但是我使用的代码只是将图像颜色变为洋红色而不是绿色

img2

Please have a look at my code: 请看一下我的代码:

func colorImage(image: UIImage) -> UIImage {
    let color = RGBA32(red: 76, green: 255, blue: 0, alpha: 255)
    let img: CGImage = image.cgImage!
    let context = CGContext(data: nil, width: img.width, height: img.height, bitsPerComponent: 8, bytesPerRow: 4 * img.width, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
    context.draw(img, in: CGRect(x: 0, y: 0, width: img.width, height: img.height))
    let binaryData = context.data!.bindMemory(to: RGBA32.self, capacity: img.width * img.height)
    for y in stride(from: 0, through: img.height, by: 10) {
        for x in 0..<img.width {
            let pixel = (y*img.width) + x
            binaryData[pixel] = color
        }
    }
    let output = context.makeImage()!
    return UIImage(cgImage: output, scale: image.scale, orientation: image.imageOrientation)
}

struct RGBA32: Equatable {
    private var color: UInt32
    init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8) {
        let red   = UInt32(red)
        let green = UInt32(green)
        let blue  = UInt32(blue)
        let alpha = UInt32(alpha)
        color = (red << 24) | (green << 16) | (blue << 8) | (alpha << 0)
    }
    static func ==(lhs: RGBA32, rhs: RGBA32) -> Bool {
        return lhs.color == rhs.color
    }
}

Tbh I'm absolutely clueless why this issue appears - so any help how to fix this would be very appreciated. Tbh我绝对不知道为什么会出现此问题-因此,对解决此问题的任何帮助将不胜感激。 Thanks :) 谢谢:)

RGBA is a confusing word. RGBA是一个令人困惑的词。 It is not clear if R is the most significant byte in the 32-bit data or the first byte in the 4-byte memory region. 还不清楚R是32位数据中的最高有效字节还是4字节存储区中的第一个字节。

But with your bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue , R takes the first place and G, B, and then Alpha last. 但是使用bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue ,R排在第一位,G,B排在最后,然后Alpha在最后。

In little-endian representation of UInt32 , R for the least significant byte. UInt32小端表示中,R表示最低有效字节。

Try this version of RGBA : 试试这个版本的RGBA

struct RGBA32: Equatable {
    private var color: (red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8)
    init(red: UInt8, green: UInt8, blue: UInt8, alpha: UInt8) {
        color = (red: red, green: green, blue: blue, alpha: alpha)
    }
    static func ==(lhs: RGBA32, rhs: RGBA32) -> Bool {
        return lhs.color == rhs.color
    }
}

(Memory allocations of Swift structs or tuples are not clearly defined, but the above code works as expected.) (Swift结构或元组的内存分配没有明确定义,但是上面的代码按预期工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM