1

我目前正在尝试使用来自 github 的 firebase 存储的快速入门示例。您只需上传图像,然后在加载该视图后将其下载到另一个视图。我可以将图像上传到存储中,但是当我尝试下载图像时会发生此错误。

错误域=FIRStorageErrorDomain 代码=-13000 “发生未知错误,>请检查服务器响应。” UserInfo={bucket=****.appspot.com, >object=379f921d-a0bb-44b5-b04e-f21cc7953848/485423329797/IMG_0003.JPG, ResponseErrorDomain=NSCocoaErrorDomain, NSDestinationFilePath=/file:/Users/mark**** **/Library/Developer/CoreSim>ulator/Devices/B600E8B9-95ED-4963-8282-9CDD43B7C25D/data/Containers/Data/Application/8FFB7EB0-0AFD-4E10-AAB6-D7340F8E3DDB/Documents/myimage.jpg, NSLocalizedDescription=An发生未知错误,请检查服务器响应。, NSUserStringVariant=( Move ), NSSourceFilePathErrorKey=/Users/mark******/Library/Developer/CoreSimula>tor/Devices/B600E8B9-95ED-4963-8282->9CDD43B7C25D /data/Containers/Data/Application/8FFB7EB0-0AFD-4E10-AAB6->D7340F8E3DDB/tmp/CFNetworkDownload_5ZmlMp.tmp,

我已经检查了 Firebase 存储端的路径并且文件路径是正确的,它似乎无法检索图像。

下载图片的代码在下载文件的 viewdidload() 函数中

override func viewDidLoad() {
super.viewDidLoad()
storageRef = FIRStorage.storage().reference()

let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,
    NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory = paths[0]
let filePath = "file:\(documentsDirectory)/myimage.jpg"
let storagePath = NSUserDefaults.standardUserDefaults().objectForKey("storagePath") as! String

print("---------------")
print(filePath)
print("---------------")
print(storagePath)

// [START downloadimage]
storageRef.child(storagePath).writeToFile(NSURL.fileURLWithPath(filePath),
                                          completion: { (url, error) in
  if let error = error {
    print("Error downloading:\(error)")
    self.statusTextView.text = "Download Failed"
    return
  }
  self.statusTextView.text = "Download Succeeded!"
  self.imageView.image = UIImage.init(contentsOfFile: filePath)
})
// [END downloadimage]

} }

4

1 回答 1

5

看起来这里的问题实际上是您的下载文件路径不正确(引发的错误实际上是NSCocoaErrorDomain网络问题而不是网络问题——看起来我们的错误消息对网络来说太具体了)。

我看到的主要问题是您的文件路径看起来像/file:/Users/...,而我认为文件 URL 应该看起来像file:///Users/...

我通常像这样创建本地文件:

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"hello"] URLByAppendingPathExtension:@"txt"];

您还可以使用NSHomeDirectoryForUser来获取用户的基本目录。

于 2016-05-22T17:02:43.413 回答