我目前的代码适用于ios.But,因为很多人都知道UIKit不在Mac OSX应用程序中。如何用我添加的子视图保存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!
}
答案 0 :(得分:0)
我已经从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等
我希望这有帮助!