图像无法从Firebase存储下载

时间:2016-05-31 07:44:54

标签: ios swift firebase firebase-storage

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

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&#34 ;操作不被允许"},ResponseErrorCode = 513})

我在stackoverflow上发现了this问题,但这是一个不同的错误(响应代码518,而我有513),并且由于我直接使用示例代码,这应该可以正常工作。

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:4)

此处的问题是,您收到NSPOSIXErrorDomain错误,表明您无权写入文件file:///local/tumbnails/\(imageName),大概是因为该目录({{1} }}}不存在,即使它存在,你也没有权限写入它。

我要查看the docs以获取您可以写入的目录列表 - 您可能应该使用/local/Documents

答案 1 :(得分:0)

好像你可能没有正确宣布你的storageRef(即你的名字叫bucket=yourbucket.appspot.com},或者你可能已经Firebase/Storage添加了workspace但未更新Google-info.plist。也许您可以展示更多代码?

更新: 你可以尝试

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

答案 2 :(得分:0)

也许它是iOS 9的App Transport Security,将此代码添加到info.plist并再次检查

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