首页 > 解决方案 > Swift UIImageView Firebase DispatchQueue

问题描述

我正在使用 firebase 来保存和加载我的图像。我在 Xcode 中创建了一个新视图,并且正在使用与加载配置文件图像相同的代码。然而,这现在抛出一个错误,说 url 字符串是 nil。图片 url 数据在“DispatchQueue.global().async”之后消失。这可能是什么原因造成的,我该如何跟踪?很奇怪,这段代码如何适用于其他视图,但对于这个新视图,它会引发错误。

let businessProfilePicture = dictionary["profPicString"] as! String
      if businessProfilePicture.count > 0 {
        let url = URL(string: businessProfilePicture)
        print(url)
        print("printing the url here to check")
        DispatchQueue.global().async {
          let dataURL = try? Data(contentsOf: url!)
          print(dataURL)
          print("printing the data url here")
          DispatchQueue.main.async {
            print(dataURL)
            print("Printing Data to check")
            let image = UIImage(data: dataURL!)?.potter_circleo
            self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
            self.businessProfilePicture.image = image
          }
        }

在此处输入图像描述

在此处输入图像描述

完整代码

func getWorkLocation() {
    let uid = Auth.auth().currentUser?.uid
    var profPicURL: String = ""
    
    Database.database().reference().child("employees").child(uid!).child("Business").observe(.value, with: { snapshot in
        if snapshot.exists() {
            let dictionary = snapshot.value as? NSDictionary
                self.businessName.text = dictionary?["businessName"] as? String
                self.businessStreet.text = dictionary?["businessStreet"] as? String
                self.businessCity.text = dictionary?["businessCity"] as? String
                profPicURL = dictionary?["profPicString"] as! String
                
                // set image
                if profPicURL.count > 0 {
                    let url = URL(string: profPicURL)
                    DispatchQueue.global().async {
                        let data = try? Data(contentsOf: url!)
                        DispatchQueue.main.async {
                            let image = UIImage(data: data!)?.potter_circle
                            self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
                            self.businessProfilePicture.image = image
                        }
                    }
                } else {
                    let image = UIImage(named: "profile picture")?.potter_circle
                    self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
                    self.businessProfilePicture.image = image
                }
                 
        } else {
            self.businessName.text = ""
            self.businessStreet.text = "Go to Add Work Location to send request"
            self.businessCity.text = ""
            self.deleteButton.isEnabled = false
        }
    })
}

标签: swiftxcodedispatch-queue

解决方案


您确定您创建的 URLprofPicURL是正确创建的吗?

URL(string:)可以失败并返回nil。如果您继续隐式打开它,Data(contentsOf: url!)您将崩溃。

同样,try? Data(contentsOf: url)可以返回 nil。如果是这样,那么当您隐式打开它时,UIImage(data: data!)您将崩溃。

正如 Jacob 在评论中所说,您需要了解有关隐式展开的选项的更多信息。为了让你开始,你可能会像这样构建你的代码:

if let url = URL(string: profPicURL) {
    DispatchQueue.global().async {
        if let data = try? Data(contentsOf: url),
           let image = UIImage(data: data)?.potter_circle
        {
            DispatchQueue.main.async {
                self.businessProfilePicture.contentMode = UIView.ContentMode.scaleAspectFill
                self.businessProfilePicture.image = image
            }
        } else {
            // raise an an error or set self.businessProfilePicture.image to a generic image or something
        }
    }
} else {
    // raise an an error or set self.businessProfilePicture.image to a generic image or something
}

推荐阅读