从Swift 3中的firebase中检索图像

时间:2017-08-07 21:06:47

标签: ios swift firebase firebase-realtime-database

图像选择器工作正常,我目前在firebase上有profileImageURL。我不知道如何从firebase检索图像,以便保存到应用程序。这就是我将它保存到firebase的方法:

extension Profile: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        selectedPhoto = info[UIImagePickerControllerEditedImage] as? UIImage
        picker.dismiss(animated: true, completion: nil)

        var data = NSData()
        data = UIImageJPEGRepresentation(selectedPhoto, 1.0)! as NSData
        // set upload path
        let filePath = "\(userId)/\("profileImage")"
        let metaData = StorageMetadata()
        metaData.contentType = "image/jpg"
        self.storageRef.child(filePath).putData(data as Data, metadata: metaData){(metaData,error) in
            if let error = error {
                print(error.localizedDescription)
                return
            }else{
                //store downloadURL
                let downloadURL = metaData!.downloadURL()!.absoluteString
                //store downloadURL at database
                self.ref?.updateChildValues(["profileImage": downloadURL])
            }
        }
    }
}

这是我尝试检索它:

func loadUserProfileImage() {
        ref?.observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.hasChild("profileImage") {
                let filePath = "\(self.userId!)/\("profileImage")"
                self.storageRef.child(filePath).getData(maxSize: 10 * 1024 * 1024, completion: { (data, error) in

                    let thisPhoto = UIImage(data: data!)
                    selectedPhoto.image = thisPhoto

                })
            }
        })
    }

0 个答案:

没有答案