我正在开发一个MacOS应用程序,用于编辑图像的元数据。为此,我使用CGImageDestinationCreateWithURL
函数来创建“文档”或“桌面”中文件的目标,但它返回nil
值。我多次检查了函数的参数(URL和文件类型),但找不到问题的原因。
@IBAction func browseFile(_ sender: AnyObject) {
let dialog = NSOpenPanel()
dialog.title = "Choose a .txt file"
dialog.showsResizeIndicator = true
dialog.showsHiddenFiles = false
dialog.canChooseDirectories = true
dialog.canCreateDirectories = true
dialog.allowsMultipleSelection = false
dialog.allowedFileTypes = nil
if dialog.runModal() == NSApplication.ModalResponse.OK {
let result = dialog.url // Pathname of the file
if result != nil {
let path = result!.path
filename_field.stringValue = path
let imageSource = CGImageSourceCreateWithURL(result as! CFURL, nil)
let type = CGImageSourceGetType(imageSource!)
let uurl = NSURL(fileURLWithPath: path)
// here I am trying to create a destination but it returns nil
var imageDest: CGImageDestination = CGImageDestinationCreateWithURL(uurl, type!, 1, nil)!
}
} else {
// User clicked on "Cancel"
return
}
}