我在教程课程中遇到了xcode问题

时间:2018-10-03 05:41:40

标签: firebase firebase-realtime-database swift4 firebase-storage xcode9

我有一个代码,希望有人帮我检查一下。由于firebase 4语法,它显然不起作用,但是我没有将其更新为5的经验。我正在学习一个教程,但是它也在4中,并且辅导老师没有回答问题。该代码的主要目的是能够手动选择要导入为个人资料图片的图像,然后上传到我的Firebase存储。请有人帮助,我将非常感谢。

import UIKit
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
class SignUpViewController: UIViewController {
    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!

    @IBOutlet weak var profileImage: UIImageView!

    var selectedImage: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()

       usernameTextField.backgroundColor = UIColor.clear
    usernameTextField.tintColor = UIColor.white
    usernameTextField.textColor = UIColor.white
    usernameTextField.attributedPlaceholder = NSAttributedString(string: usernameTextField.placeholder!, attributes: [NSAttributedStringKey.foregroundColor: UIColor(white: 1.0, alpha: 0.6)])
    let bottomLayerUsername = CALayer()
    bottomLayerUsername.frame = CGRect(x: 0, y: 29, width: 1000, height: 0.6)
    bottomLayerUsername.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 25/255, alpha: 1).cgColor
    usernameTextField.layer.addSublayer(bottomLayerUsername)

    emailTextField.backgroundColor = UIColor.clear
    emailTextField.tintColor = UIColor.white
    emailTextField.textColor = UIColor.white
    emailTextField.attributedPlaceholder = NSAttributedString(string: emailTextField.placeholder!, attributes: [NSAttributedStringKey.foregroundColor: UIColor(white: 1.0, alpha: 0.6)])
    let bottomLayerEmail = CALayer()
    bottomLayerEmail.frame = CGRect(x: 0, y: 29, width: 1000, height: 0.6)
    bottomLayerEmail.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 25/255, alpha: 1).cgColor
    emailTextField.layer.addSublayer(bottomLayerEmail)

    passwordTextField.backgroundColor = UIColor.clear
    passwordTextField.tintColor = UIColor.white
    passwordTextField.textColor = UIColor.white
    passwordTextField.attributedPlaceholder = NSAttributedString(string: passwordTextField.placeholder!, attributes: [NSAttributedStringKey.foregroundColor: UIColor(white: 1.0, alpha: 0.6)])
    let bottomLayerPassword = CALayer()
    bottomLayerPassword.frame = CGRect(x: 0, y: 29, width: 1000, height: 0.6)
    bottomLayerPassword.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 25/255, alpha: 1).cgColor
    passwordTextField.layer.addSublayer(bottomLayerPassword)


        profileImage.layer.cornerRadius = 40
        profileImage.clipsToBounds = true

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

    func handleSelectProfileImageView() {
        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        present(pickerController, animated: true, completion: nil)
    }
    @IBAction func dismiss_onClick(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

    @IBAction func signUpBtn_TouchUpInside(_ sender: Any) {
        Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (authData: AuthDataResult?, error: Error?) in
            if error != nil {
                print(error!.localizedDescription)
                return
            }
            let uid = authData!.user.uid
            let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("profile_image").child(uid)
            if let profileImg = self.selectedImage, let imageData = UIImageJPEGRepresentation(profileImg, 0.1) {
               storageRef.put(imageData, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    return
                }

                let profileImageUrl = metadata?.downloadURL()?.absoluteString
                let ref = Database.database().reference()
                let usersReference = ref.child("users")
                let newUserReference = usersReference.child(uid!)
                newUserReference.setValue(["username": self.usernameTextField.text!, "email": self.emailTextField.text!, "profileImageUrl": profileImageUrl])
               })
            }
        })

0 个答案:

没有答案