【发布时间】:2018-08-23 10:25:38
【问题描述】:
我想使用我在下面添加的代码在 UIImage 上生成这个很酷的东西。 但似乎有一个与颜色有关的疯狂错误:
所以我希望我的函数用给定的 RGB 值在图像上画线(在我的例子中是 r:76, g:255, b:0 以获得这种绿色外观),因为它的颜色:
但我使用的代码只是将图像着色为洋红色而不是绿色:
请看我的代码:
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 我完全不知道为什么会出现这个问题 - 所以任何如何解决这个问题的帮助将不胜感激。谢谢:)
【问题讨论】:
-
你不觉得显示的颜色看起来像 r:255,g:0,b:255,a:76。你知道 iOS 在 little-endian CPU 上运行吗?
-
为每个像素分配一个
struct而不是struct中包含的UInt32值似乎真的很奇怪。也许我错过了什么。 -
您愿意分享一些关于您的想法的代码吗? @rmaddy
-
无需处理像素,只需将原始图像绘制到图像图形上下文中,在其上绘制线条,然后提取结果图像。完成。
-
所以上面的代码只是一个示例 sn-p - 我绝对意识到“通过操纵它的像素在 UIView 上画线”是完全错误的方式但是在我的 (未共享) 代码的“操纵像素”的其他部分出现了这个颜色问题 - 这就是我问你们并添加一个简单示例以在显而易见的方式:) @matt