I am new with MACOS Development and making one app. In my application, I am opening NSOpenPanel and selecting a folder or any directory(like download, document) then NSOPenPanel begin method gives me the selected URL. I want to save multiple images on that selected URL but unable to achieve that facing this error.
The file “xxx” couldn’t be saved in the folder “xxx”.
@IBAction func menuExportTapped(_ sender: NSMenuItem) {
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = false
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = true
openPanel.title = NSLocalizedString("change_the_folder", comment: "")
openPanel.runModal()
openPanel.begin { [weak self] (result) -> Void in
if result == .OK {
self?.myImage?.writePNG(toURL: openPanel.url!)
} else {
openPanel.close()
}
}
}
I tested writePNG code it's working fine.
extension NSImage {
public func writePNG(toURL url: URL) {
guard let data = tiffRepresentation,
let rep = NSBitmapImageRep(data: data),
let imgData = rep.representation(using: .png, properties: [.compressionFactor : NSNumber(floatLiteral: 1.0)]) else {
Swift.print("\(self) Error Function '\(#function)' Line: \(#line) No tiff rep found for image writing to \(url)")
return
}
do {
try imgData.write(to: url)
}catch let error {
Swift.print("\(self) Error Function '\(#function)' Line: \(#line) \(error.localizedDescription)")
}
}
}
Please help me with this. Thank You!