我似乎无法理解在Swift中手动访问图像像素数据的方法。 我正在尝试从CGImage创建图像掩码,以便稍后可以在单独的图像上使用它。 我想识别特定值的所有像素,并将图像中的其他所有内容转换为黑白或透明度(目前并不重要)。 我正在尝试的代码如下:
let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
let contextWidth: Int = Int(snapshot.size.width)
let contextHeight: Int = Int(snapshot.size.height)
let bytesPerPixel: Int = 24
let bitsPerComponent: Int = 8
let bytesPerRow: Int = bytesPerPixel * contextWidth
let bitmapInfo: CGBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue)
guard let context: CGContext = CGContext(data: nil, width: contextWidth, height: contextHeight, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) else {
print("Could not create CGContext")
return
}
context.draw(maskCGImage, in: CGRect(x: 0, y: 0, width: contextWidth, height: contextHeight))
guard let contextDataRaw: UnsafeMutableRawPointer = context.data else {
print("Could not get UnsafeMutableRawPointer from CGContext")
return
}
let contextData: UnsafeMutablePointer<UInt8> = contextDataRaw.bindMemory(to: UInt8.self, capacity: contextWidth * contextHeight)
for row in 0..<contextHeight {
for col in 0..<contextWidth {
let offset = (col * contextHeight) + row
let pixelArray = [contextData[offset], contextData[offset + 1], contextData[offset + 2]]
if pixelArray == [120, 120, 120] {
contextData[offset] = 0
contextData[offset + 1] = 0
contextData[offset + 2] = 0
}
}
}
我尝试了各种行列排列方式,以确定正确的顺序,即
let offset = (row * contextWidth) + col
、let offset = (col * contextHeight) + row
、let offset = ((row * contextWidth) + col) * 3
、let offset = ((row * contextWidth) + col) * 4
。我的输出结果类似于这样(请记住,此图像应该看起来像一团随机颜色的斑点): 如我漂亮的小箭头所示,顶部的黑色样本是我编辑过的像素,这些像素确实应该变成黑色,然而所有其他灰色像素(例如箭头下面的像素)也应该变成黑色。它们的RGB值肯定是相同的120, 120, 120。
我知道问题在于我遍历数组的顺序,但似乎无法找出模式。另外需要注意的是,使用copy(maskingColorComponents:)不行,因为我想删除一些特定的颜色,而不是一系列颜色。
非常感谢您的帮助。提前致谢!
for row in 0..<contextHeight
并且只是一次添加一个row
到偏移量,但是在我看来,一个row
跳跃的大小需要是一行中所有字节的大小。 - mattlet offset = (col * contextHeight) + (row * numComponents)
。这样可以快速跳到正确的行,然后以4个字节为步长在该行中移动。如果你愿意将其写成答案,我可以将其标记为正确解决方案。 - MichaelbytesPerPixel
应该是 3 而不是 24 吗? - pbouf77