MacOS 的 GPUImage - 如何将处理后的文件保存到磁盘

GPUImage with MacOS - how to save processed file to disk

我目前正在将一些代码从 CoreImage 移植到 GPUImage,但在将处理后的文件保存到磁盘时遇到问题。虽然这适用于 CoreImage:

guard
    let blurFilter = CIFilter(name: "CIGaussianBlur"),
    let imageURL = Bundle.main.url(forResource: "my-image", withExtension: "png"),
    let ciImage = CIImage(contentsOf: imageURL)
else {
    return
}

blurFilter.setValue(ciImage, forKey: kCIInputImageKey)
blurFilter.setValue(100, forKey: kCIInputRadiusKey)

guard
    let result = blurFilter.outputImage,
    let cgImage = context.createCGImage(result, from: result.extent)
else {
    return
}

let destinationURL = desktopURL.appendingPathComponent("output.png")
let nsImage = NSImage(cgImage: cgImage, size: ciImage.extent.size)

以下代码移植到GPUImage:

let testImage = NSImage(named:"my-image.png")!
let blurFilter = GaussianBlur()
let filteredImage = testImage.filterWithOperation(blurFilter)

let desktopURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!
let destinationURL = desktopURL.appendingPathComponent("output.png")
let nsImage = NSImage(nsImage: filteredImage, size: filteredImage.extent.size)

给出与最后一行有关的以下错误消息:

Value type of 'NSImage' has no member 'extent'

filteredImage 是一个 NSImage。在您的 Core Image 代码中,ciImage 是一个 CIImage。 CIImage有extent属性,NSImage没有

不需要在你的第二个代码中创建 nsImagefilteredImage 已经是一个 NSImage。大功告成。