不从Firebase存储下载的图像

我正尝试从新的Firebase存储中将图像下载到本地文件。 为此,我使用Firebase提供的示例。 这是我的代码:

func getTumbnails(imageName: String) { // Create a reference to the file you want to download let tumbnailRef = storageRef.child("tumbs/\(imageName)") // Create local filesystem URL let localURL: NSURL! = NSURL(string: "file:///local/tumbnails/\(imageName)") // Download to the local filesystem let downloadTask = tumbnailRef.writeToFile(localURL) { (URL, error) -> Void in if (error != nil) { print(error) } else { let data = NSData(contentsOfURL: URL!) self.data = data! print(data) } } } 

但是,当我调用函数getTumbnails("image")我得到以下错误打印到控制台:

可选(错误域= FIRStorageErrorDomain代码= -13000“未知的错误发生,请检查服务器响应。”UserInfo = {object = tumbs / Sunset.png,bucket = ********。appspot.com,NSLocalizedDescription =发生未知错误,请检查服务器响应。,ResponseErrorDomain = NSCocoaErrorDomain,NSFilePath = / local / tumbnails,NSUnderlyingError = 0x137f629c0 {Error Domain = NSPOSIXErrorDomain Code = 1“Operation not permitted”},ResponseErrorCode = 513})

我在这里find了这个问题在stackoverflow,但这是一个不同的错误(响应代码518,而我有513),因为我直接使用示例代码,这应该只是工作。

有人可以帮帮我吗?

这里的问题是,你得到一个NSPOSIXErrorDomain错误,指出你没有写入文件的权限file:///local/tumbnails/\(imageName) ,大概是因为该目录( /local )没有存在,即使这样做,你也没有写入的权限。

我会看看你可以写的目录列表的文档 – 你可能应该使用/Documents/tmp这个。

也许它是iOS 9的应用程序传输安全性,将此代码添加到您的info.plist并再次检查

  <key>NSAppTransportSecurity</key> <dict> <!--Include to allow all connections (DANGER)--> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 

看起来像你可能没有正确地声明你的storageRef (即你的bucket被称为bucket=yourbucket.appspot.com ),或者你可能已经将Firebase/Storage添加到了你的workspace但是没有更新Google-info.plist 。 也许你可以展示更多的代码?

更新:你可以试试

 let localURL: NSURL! = NSURL.fileURLWithPath(string: "file:///local/tumbnails/\(imageName)")