简体   繁体   中英

How to retrieve image from firebase storage Swift 4 ios

I am trying to retrieve an image from firebase storage but the image that I retrieve always is nil for some reason.

var ref: DatabaseReference!
var storageRef: StorageReference!
var hallData = [Hall]()
override func viewDidLoad() {
    let refHandle = Database.database().reference().child("hallData").observe(DataEventType.value, with: { (snapshot) in
        let postDict = snapshot.value as? [String : AnyObject] ?? [:]
         let values = Array(postDict.values)
        //print(values)
        let valueDict = values as! [[String:Any]]

        for i in valueDict
        {
            var name = i["name"] as! String
            var address = i["address"] as! String
            var capacity = i["capacity"] as! String
            var decorations = i["decorations"] as! String
            var highPrice = i["highPrice"] as! String
            var lowPrice = i["lowPrice"] as! String
            var catering = i["catering"] as! String
            var email = i["email"] as! String
            self.storageRef = Storage.storage().reference().child("images").child(email)
            var image: UIImage!
            // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)

            self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in

                if let error = error {
                    print("PLASESEE")
                    print(error.localizedDescription)
                    // Uh-oh, an error occurred!
                } else {
                    // Data for "images/island.jpg" is returned
                    let image = UIImage(data: data!)
                }
            }
        print(image)
            self.hallData.append(Hall(name2: name, capacity2: capacity, lowPrice2: lowPrice, highPrice2: highPrice, catering2: catering,decorations2: decorations, address2:address, image2: image, email2: email))
        }
    })
}

I dont understand what I am doing wrong, I followed the api on firebase storage, checked out a lot of tutorials but I keep getting nil

The issue is that you're attempting to work with the image var outside the getData closure.

    self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
            if let error = error {
                print("PLASESEE")
                print(error.localizedDescription)
                // Uh-oh, an error occurred!
            } else {
                // Data for "images/island.jpg" is returned
                let image = UIImage(data: data!)
            } <- closure ends here and image is only valid above this
        }
        print(image) <- image may not be populated at this point
        self.hallData.append... image
    }

That closure is asynchronous and the call to self.hallData.append... will occur way before the image var is populated within the closure. Code is much faster than the internet

Move that statement inside the closure, right after the let image = and it should work.

       self.storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
           if let error = error {
               print("An error occurred in downloading the image")
               print(error.localizedDescription)
           } else {
               let image = UIImage(data: data!)
               self.hallData.append... image
               //reload your tableView or UI as self.hallData is now valid
           }
       }

If you do it this way, the preceeding var image: UIImage! can be removed as it doesn't have a function. Otherwise, remove the let before let image = within the closure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM