gpt4 book ai didi

swift - 如何使用 subview 在 Mac OSX 中保存 NSI​​mage?

转载 作者:行者123 更新时间:2023-11-28 08:19:18 25 4
gpt4 key购买 nike

我有适用于 ios 的当前代码。但是你们中的许多人都知道 UIKit 不在 Mac OSX 应用程序中。我如何保存带有我添加的 subview 的 nsimage

private func screenShotMethod() -> NSImage? {

//Create the UIImage
NSGraphicsBeginImageContext(self.pickedImge.frame.size)
self.pickedImge.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = NSGraphicsGetImageFromCurrentImageContext()
NSGraphicsEndImageContext()

//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
return image!
}

最佳答案

我从我在 this very related question 中找到的代码移植了这个答案.

class CustomView : NSView {

func saveAScreenShotOfThisViewSomewhere() -> Void {
self.lockFocus()
let rep = NSBitmapImageRep(focusedViewRect: self.bounds)
self.unlockFocus()
if let data = rep?.tiffRepresentation {
do {
try data.write(to: URL(string:"/tmp/someplace.tiff")!, options: Data.WritingOptions.atomic)
} catch {
Swift.print("error in writing -> \(error.localizedDescription)")
}
}
}
}

你也可以使用

    if let data = rep?.representation(using: .JPEG, properties: [String : Any]) {

}

其中表示形式包括 BMP、GIF、JPEG、PNG、TIFF 等

希望对您有所帮助!

关于swift - 如何使用 subview 在 Mac OSX 中保存 NSI​​mage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41710170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com