swift - NSBitmapImageRep 上的 setColor 在 Swift 中不起作用
问题描述
我试图弄清楚 setColor 是如何工作的。我有以下代码:
lazy var imageView:NSImageView = {
let imageView = NSImageView(frame: view.frame)
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
createColorProjection()
view.wantsLayer = true
view.addSubview(imageView)
view.needsDisplay = true
}
func createColorProjection() {
var bitmap = NSBitmapImageRep(cgImage: cgImage!)
var x = 0
while x < bitmap.pixelsWide {
var y = 0
while y < bitmap.pixelsHigh {
//pixels[Point(x: x, y: y)] = (getColor(x: x, y: y, bitmap: bitmap))
bitmap.setColor(NSColor(cgColor: .black)!, atX: x, y: y)
y += 1
}
x += 1
}
let image = createImage(bitmap: bitmap)
imageView.image = image
imageView.needsDisplay = true
}
func createImage(bitmap:NSBitmapImageRep) -> NSImage {
let image = bitmap.cgImage
return NSImage(cgImage: image! , size: CGSize(width: image!.width, height: image!.height))
}
代码的目的是将照片(彩虹)更改为全黑(我现在只是用黑色进行测试,以确保我理解它是如何工作的)。但是,当我运行程序时,显示的是彩虹不变的图片,而不是黑色照片。
我收到这些错误:
Unrecognized colorspace number -1
和Unknown number of components for colorspace model -1
.
谢谢。
解决方案
首先,你是对的:setColor
至少从 Catalina 开始就被打破了。Apple 没有修复它可能是因为它太慢且效率低下,而且没有人使用它。
其次,文档说NSBitmapImageRep(cgImage: CGImage)
会生成一个只读位图,因此即使有效,您的代码也不会setColor
有效。
正如亚历山大所说,自己制作CIFilter
是将照片像素更改为不同颜色的最佳方式。编写和实现 OpenGL 并不容易,但它是最好的。
如果您要NSBitmapImageRep
像这样添加扩展名:
extension NSBitmapImageRep {
func setColorNew(_ color: NSColor, atX x: Int, y: Int) {
guard let data = bitmapData else { return }
let ptr = data + bytesPerRow * y + samplesPerPixel * x
ptr[0] = UInt8(color.redComponent * 255.1)
ptr[1] = UInt8(color.greenComponent * 255.1)
ptr[2] = UInt8(color.blueComponent * 255.1)
if samplesPerPixel > 3 {
ptr[3] = UInt8(color.alphaComponent * 255.1)
}
}
}
然后可以像这样简单地更改图像的像素:
func changePixels(image: NSImage, newColor: NSColor) -> NSImage {
guard let imgData = image.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: imgData),
let color = newColor.usingColorSpace(.deviceRGB)
else { return image }
var y = 0
while y < bitmap.pixelsHigh {
var x = 0
while x < bitmap.pixelsWide {
bitmap.setColorNew(color, atX: x, y: y)
x += 1
}
y += 1
}
let newImage = NSImage(size: image.size)
newImage.addRepresentation(bitmap)
return newImage
}