【问题标题】:Images not downloading from Firebase Storage图片未从 Firebase 存储下载
【发布时间】:2016-09-29 02:48:31
【问题描述】:

我正在尝试将图像从新的 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") 时,控制台会打印出以下错误:

Optional(Error Domain=FIRStorageErrorDomain Code=-13000 "发生未知错误,请检查服务器响应。" UserInfo={object=tumbs/Sunset.png, bucket=******** .appspot.com, NSLocalizedDescription=发生未知错误,请检查服务器响应。, ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/local/tumbnails, NSUnderlyingError=0x137f629c0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not allowed"}, ResponseErrorCode=第513章)

我在 stackoverflow 上找到了 this 问题,但这是一个不同的错误(响应代码 518,而我有 513),因为我直接使用示例代码,所以应该可以。

有人可以帮帮我吗?

【问题讨论】:

    标签: ios swift firebase firebase-storage


    【解决方案1】:

    似乎您可能没有正确声明您的storageRef(即您的存储桶称为bucket=yourbucket.appspot.com),或者您可能将Firebase/Storage 添加到您的workspace 但没有更新Google-info.plist。或许您可以展示更多代码?

    更新: 你可以试试

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

    【讨论】:

    • 谢谢,但我已经正确设置了一切。出于安全原因,我将其更改为yourbucket.appspot.com,因为我还没有任何规则。
    • 或许你可以试试let localURL: NSURL! = NSURL.fileURLWithPath(string: "file:///local/tumbnails/\(imageName)")
    • 这不是 Firebase 存储问题,错误(令人困惑)是 NSPOSIXErrorDomain,这意味着问题来自文件系统,不允许开发人员写入该位置。
    • 完全正确。我环顾四周,其中一个修复似乎是.fileURLWithPath
    【解决方案2】:

    可能是 iOS 9 的 App Transport Security,将此代码添加到您的 info.plist 并再次检查

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

    【讨论】:

    • 不,ATS 不是必需的,因为 Firebase 存储使用适当的 SSL 密码。
    【解决方案3】:

    这里的问题是您收到NSPOSIXErrorDomain 错误,表明您无权写入文件file:///local/tumbnails/\(imageName),可能是因为该目录(/local)不存在,即使确实如此,但您没有写入权限。

    我会查看the docs 以获取您可以写入的目录列表——您可能应该为此使用/Documents/tmp

    【讨论】:

    • 谢谢!但是,这适用于模拟器,但不适用于实际设备。
    • 模拟器上的 App Sandbox 可能与设备上的运行方式不同 - 您在设备上遇到什么问题?您也可能在设备上以不同的权限运行,并且无法写入/Documents(您可能必须写入您的用户目录或仅写入/tmp
    • 尝试写信给/tmp时出现的错误是一样的吗?设备越狱了吗?
    • 是的,同样的错误,我的设备没有越狱
    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2020-02-08
    • 2017-05-16
    • 2019-10-04
    • 2021-02-13
    相关资源
    最近更新 更多