NSData writeToFile方法返回true但文件未写入磁盘

Ran*_*ndy 2 macos nsdata writetofile swift osx-yosemite

我开发了一个OS X应用程序,有时我需要将一些图片保存到磁盘上.我有一个方法来做到这一点:

func saveImageAtPath(image: NSImage, path: String)
{
    if let data = image.TIFFRepresentation
    {
        let bitmap = NSBitmapImageRep(data: data)
        let pngImage = bitmap?.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:])

        NSLog("Path : %@", path)

        if let _ = pngImage?.writeToFile(path, atomically: false)
        {
            NSLog("Everything should work...")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试保存图像时,一切似乎都有效,控制台中会显示以下文本:

路径:/Users/myusername/Documents/Pictures/Apps/testPictures/mypicture.png

一切都应该工作......

一个月前一切都很完美.这是我的conf:

OS X Yosemite 10.10.5 Xcode 7.2

如果有人有想法......那太棒了!

提前致谢.

小智 11

我花了一整天的时间试图弄清楚如何在swift 2.0中编写建议的修补程序,所以我想现在分享我已经有了它的工作

do{
    let result = try Bool(pngImage!.writeToFile(filePath, options: NSDataWritingOptions.DataWritingAtomic))
}
catch let error as NSError {
    print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)

我希望能节省一些时间.