使用栏项目按钮将个人资料照片和封面照片保存到Firebase

时间:2019-11-14 06:36:47

标签: ios swift firebase

因此,我正在为我的应用程序使用“编辑配置文件”视图控制器。我目前已在用户可以更改个人资料图片和封面照片的地方进行设置。我正在制作两张图片,无论是同时更改它们还是只更改其中一张,保存到我的Firebase存储和数据库中。我有一个指示“保存”的工具栏操作按钮,我希望用户能够在完成操作后按下该按钮,并保存所做的更改并将其定向回常规配置文件视图控制器。这是我目前拥有的代码,但似乎无法正常工作。我按下了保存按钮,但没有任何改变,它仍然使我保持在同一视图控制器上,而没有将任何内容上传到Firebase。我也不确定是否需要调用两个单独的函数“ uploadProfileImage”和“ uploadCoverImage”。

import UIKit
import Foundation
import Firebase
import FirebaseDatabase

class NewEditProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!

    var imagePicker = UIImagePickerController()
    var imagePicked = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = true
    }


    @IBAction func chooseImage1(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
            present(imagePicker, animated: true)
        }

        }

    @IBAction func chooseImage2(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
                present(imagePicker, animated: true)
    }

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage

        if imagePicked == 1 {
            imageView1.image = pickedImage
        } else if imagePicked == 2 {
            imageView2.image = pickedImage
        }
        dismiss(animated: true)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }

@IBAction func saveButton(_ sender: Any) {

  func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
  guard let uid = Auth.auth().currentUser?.uid else { return }
  let storageRef = Storage.storage().reference().child("user/\(uid)")

    guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }

  let metaData = StorageMetadata()
  metaData.contentType = "image/jpg"

  storageRef.putData(imageData, metadata: metaData) { metaData, error in
      if error == nil, metaData != nil {

          storageRef.downloadURL { url, error in
              completion(url)
              // success!
          }
          } else {
              // failed
              completion(nil)
            }
        }
    }

    func uploadCoverImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child("user/\(uid)")

      guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        if error == nil, metaData != nil {

            storageRef.downloadURL { url, error in
                completion(url)
                // success!
            }
            } else {
                // failed
                completion(nil)
              }
          }
      }

    func saveProfile(profileImageURL:URL, coverImageURL: URL, completion: @escaping((_ success:Bool)->())) {
        guard let uid = Auth.auth().currentUser?.uid else { return }

        let databaseRef = Database.database().reference().child("users/profile/\(uid)")

        let userObject = [
            "profilePhotoURL": profileImageURL.absoluteString,
            "coverPhotoURL": coverImageURL.absoluteString
        ] as [String:Any]

        databaseRef.setValue(userObject) {error, ref in
            completion(error == nil)
            }
        }
    }
}

0 个答案:

没有答案