Swift 3 - How to download Profile Image from FireBase Storage

余生长醉 提交于 2019-12-11 05:52:51

问题


I need help on retrieving Image from firebase storage, i learned how to save it but cant download it to current user profile. here is my code so far:

FIRAuth.auth()?.createUserWithEmail(email!, password: password!, completion: { (authData, error) in
            if error == nil {

                if (password == "" || name == "" || email == "") {
                    self.showAlert("error", message: " Please Fill In The Blank")
                }
                if (password != confirm_password) {
                    self.showAlert("ERROR", message: "Password Don't Match")
                }

            }
            else {
                self.showAlert("ERROR", message: "Please Try Again")
            }

            let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")"
            var data = NSData()
            let metaData = FIRStorageMetadata()
            //let imageName = NSUUID().UUIDString
            let storageRef = FIRStorage.storage().referenceForURL("gs://storageURL")
            storageRef.child(filePath).putData(data, metadata: metaData){(metaData,error) in
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                if let ProfileImageUrl = metaData?.downloadURL()?.absoluteString {
                    let values : [String : AnyObject] = ["name": name!, "email": email!, "profileImageUrl": ProfileImageUrl]
                    self.userPost(values)
                }

            }


        })

    }
    //save data in Database
    func userPost(values: [String: AnyObject]) {

        let ref = FIRDatabase.database().referenceFromURL("https://databaseURL.firebaseio.com/")
        ref.child("users").childByAutoId().setValue(values)
    }

so i got that so far but cant figure out how to download it to user profile. here is my code for user ProfileVC:

@IBOutlet weak var ProfileImg: UIImageView!

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.retriveData()
        ProfileImg.image = UIImage(named: "ic_account_circle.png")
        imagePicker.delegate = self
    }

 func DownloadProfilePhoto() {
        let storageRef = FIRStorage.storage().referenceForURL("gs://StorageURL.com")
        let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")"

    }

Please Help.....


回答1:


You need to download the image data, and then create an image. You should try this:

let storage = FIRStorage.storage()
var reference: FIRStorageReference!
reference = self.storage.referenceForURL("gs://appname.appspot.com/filePath")
reference.downloadURLWithCompletion { (url, error) in
    let data = NSData(contentsOfURL: url!)
    let image = UIImage(data: data!)
    ProfileImg.image = image 
}

You should check the Firebase documentation https://firebase.google.com/docs/storage/ios/download-files




回答2:


For people looking for swift 4

let storage = Storage.storage()
var reference: StorageReference!
reference = storage.reference(forURL: "gs://appname.appspot.com/filePath")
reference.downloadURL { (url, error) in
    let data = NSData(contentsOf: url!)
    let image = UIImage(data: data! as Data)
    cell.imgOutlet.image = image

}


来源:https://stackoverflow.com/questions/39459555/swift-3-how-to-download-profile-image-from-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!