将图像上传到Firebase存储并显示为配置文件图像

时间:2019-06-01 11:10:49

标签: swift firebase uiimageview uiimagepickercontroller firebase-storage

我需要有关将图像上传到Firebase存储的帮助。我的应用程序中有一个配置文件菜单。当用户点击此菜单时,他/她可以看到个人资料及其信息和个人资料图像。所以我做到了,所以您可以从图库中选择一张照片。但是我需要将照片保存到Firebase存储,并通过uid将引用添加到Firebase数据库。 另外,用户可能没有照片,因此它将是零,因为数据库中没有任何照片。看看照片,您会了解一切

extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    var selectedImage: UIImage?
    if let editedImage = info[.editedImage] as? UIImage {
        selectedImage = editedImage
        self.profileImage.image = selectedImage!
        self.savedImage = selectedImage
        picker.dismiss(animated: true, completion: nil)
    } else if let originalImage = info[.originalImage] as? UIImage {
        selectedImage = originalImage
        self.profileImage.image = selectedImage!
        self.savedImage = selectedImage
        picker.dismiss(animated: true, completion: nil)
    }

}



let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView) )
        profileImage.addGestureRecognizer(tapGesture)
        profileImage.isUserInteractionEnabled = true

@objc func handleSelectProfileImageView () {
    print("Tapped")

    let pickerController = UIImagePickerController()
    pickerController.delegate = self
    present(pickerController, animated: true, completion: nil)

}

因此,如何上传和下载图像。 如果用户没有照片(数据库中的引用)。他将从资产中看到图像。 如果用户有照片,他将看到来自FB的图像。 image

1 个答案:

答案 0 :(得分:1)

用于上传到Firebase存储

let imgData: NSData = NSData(data: UIImageJPEGRepresentation((self.img_Photo?.image)!, 0.5)!)
let _:NSData = NSData(data:UIImagePNGRepresentation(((self.img_Photo?.image)!))!)
self.uploadProfileImageToFirebase(data: imgData)

上传功能

func uploadProfileImageToFirebase(data:NSData){
    let randomPic = randomString(length: 10)
    let storageRef = Storage.storage().reference().child("Pictures").child("\(value(forKey: "UserUID") ?? randomPic).jpg")
    if data != nil {
        storageRef.putData(data as Data, metadata: nil, completion: { (metadata, error) in
            if(error != nil){
                print(error)
                return
            }
            guard let userID = Auth.auth().currentUser?.uid else {
                return
            }
            // Fetch the download URL
            storageRef.downloadURL { url, error in
                if let error = error {
                    // Handle any errors
                    if(error != nil){
                        print(error)
                        return
                    }
                } else {
                    // Get the download URL for 'images/stars.jpg'

                    let urlStr:String = (url?.absoluteString) ?? ""
                    let values = ["photo_url": urlStr]
                    self.registerUserIntoDatabaseWithUID(uid: userID, values: values as [String : AnyObject])
                }
            }
        })
    }

}

func registerUserIntoDatabaseWithUID(uid:String, values:[String:AnyObject]){
    let ref = Database.database().reference(fromURL: "https://domain.firebaseio.com/")
    let usersReference = ref.child("users").child((Auth.auth().currentUser?.uid)!)

    usersReference.updateChildValues(values) { (error, ref) in
        if(error != nil){
            print(error)
            return
        }
        self.parentVC?.dismiss(animated: true, completion: nil)
    }

}