Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MacOS上的Swift-如何将NSImage保存到磁盘_Swift_Macos_Macros_Nsimage - Fatal编程技术网

MacOS上的Swift-如何将NSImage保存到磁盘

MacOS上的Swift-如何将NSImage保存到磁盘,swift,macos,macros,nsimage,Swift,Macos,Macros,Nsimage,我的代码如下: let myImage = NSImage(named: "my-image.png") filter.setValue(myImage, forKey: kCIInputImageKey) filter.setValue(0.5, forKey: kCIInputIntensityKey) let resultImage = filter.outputImage 如何将过滤后的图像(作为PNG)保存到磁盘?请注意,这是一个MacOS版本,其中UIImage不可用(Xcod

我的代码如下:

let myImage = NSImage(named: "my-image.png")

filter.setValue(myImage, forKey: kCIInputImageKey)
filter.setValue(0.5, forKey: kCIInputIntensityKey)

let resultImage = filter.outputImage

如何将过滤后的图像(作为PNG)保存到磁盘?请注意,这是一个MacOS版本,其中UIImage不可用(Xcode抛出:尝试导入时没有此类模块“UIImage”)

您可以创建核心映像上下文,并根据ciimage筛选结果创建CGImage。您可以按如下方式进行操作:

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let context = CIContext()
        let desktopURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!
        guard
            let filter = CIFilter(name: "CISepiaTone"),
            let imageURL = Bundle.main.url(forResource: "my-image", withExtension: "png"),
            let ciImage = CIImage(contentsOf: imageURL)
        else { return }

        filter.setValue(ciImage, forKey: kCIInputImageKey)
        filter.setValue(0.5, forKey: kCIInputIntensityKey)

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

        let destinationURL = desktopURL.appendingPathComponent("my-image.png")
        let nsImage = NSImage(cgImage: cgImage, size: ciImage.extent.size)
        if nsImage.pngWrite(to: destinationURL, options: .withoutOverwriting) {
            print("File saved")
        }
    }
}

您需要这些扩展来获取png表示数据,以便将生成的图像写入磁盘:

extension NSImage {
    var pngData: Data? {
        guard let tiffRepresentation = tiffRepresentation, let bitmapImage = NSBitmapImageRep(data: tiffRepresentation) else { return nil }
        return bitmapImage.representation(using: .png, properties: [:])
    }
    func pngWrite(to url: URL, options: Data.WritingOptions = .atomic) -> Bool {
        do {
            try pngData?.write(to: url, options: options)
            return true
        } catch {
            print(error)
            return false
        }
    }
}

工作得很有魅力。非常感谢。