CGImageDestinationCreateWithURL返回零值

时间:2018-08-02 19:44:07

标签: swift macos core-graphics

我正在开发一个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
    }
}

0 个答案:

没有答案