将按钮图像保存到Firebase Xcode

时间:2019-05-24 04:34:07

标签: swift firebase firebase-realtime-database

我有一个UIViewController,希望用户能够向Firebase发布一些信息。我以前只有一个UIImageView,用户可以在其中上传一张照片,但是最近我将其更改为添加3。在尝试上传这三张照片时遇到了一个问题,我想知道是否有人可以看一下在我的代码,可能会帮助我。

class CustomImagePickerController: UIImagePickerController {
var imageButton: UIButton?
}

class newPostViewController: UIViewController

 var user: User?


 lazy var image1Button = createButton(selector: #selector(handleSelectPhoto))
lazy var image2Button = createButton(selector: #selector(handleSelectPhoto))
lazy var image3Button = createButton(selector: #selector(handleSelectPhoto))

@objc func handleSelectPhoto(button: UIButton) {
    print("Select photo with button:", button)
    let imagePicker = CustomImagePickerController()
    imagePicker.delegate = self
    imagePicker.imageButton = button
    present(imagePicker, animated: true)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    let selectedImage = info[.originalImage] as? UIImage
    let imageButton = (picker as? CustomImagePickerController)?.imageButton
    imageButton?.setImage(selectedImage?.withRenderingMode(.alwaysOriginal), for: .normal)
    dismiss(animated: true)

}

func createButton(selector: Selector) -> UIButton {
    let button = UIButton(type: .system)
    button.setTitle("Select Photo", for: .normal)
    button.backgroundColor = .white
    button.layer.cornerRadius = 8
    button.addTarget(self, action: selector, for: .touchUpInside)
    button.imageView?.contentMode = .scaleAspectFill
    button.clipsToBounds = true
    return button
}
    override func viewDidLoad() {
    super.viewDidLoad()
 @objc func handlePost() {
    let fromId = Auth.auth().currentUser!.uid
    let timestamp = Int(Date().timeIntervalSince1970)
    guard let image = JobImageView.image, let category = JobCategory.text, let description = JobBio.text, let cost = price.text else {
        print("Error")
        return
    }
    guard let uid = Auth.auth().currentUser?.uid  else {
        return
    }
    let imageName = NSUUID().uuidString
    let storageRef = Storage.storage().reference().child("job_images").child("\(imageName).png")
    let images = [image1Button, image2Button, image3Button]
    if let JobImageUrl = self.JobImageView.image, let  uploadData = JobImageUrl.jpegData(compressionQuality: 0.50) {
        storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in



            if error != nil, metadata != nil {
                print(error ?? "")
                return

            }

            storageRef.downloadURL(completion: { (url, error) in
                if error != nil {
                    print(error!.localizedDescription)
                    return
                }

                if let images = url?.absoluteString {
                    let values = ["category": category, "description": description, "cost": cost, "images": images, "fromId": fromId, "timestamp": timestamp] as [String : Any]
                    self.registerUserIntoDatabseWithUID(uid: uid, values: values as [String : AnyObject])
                }
            })
        })
    }

}


private func registerUserIntoDatabseWithUID(uid: String, values: [String: AnyObject]) {
    let ref = Database.database().reference(fromURL: "https://odd-jobs-llc-f854a.firebaseio.com/")
    let usersReference = ref.child("JobPost").child(uid)
    usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
        if err != nil {
            print("err")
            return
        }

        self.dismiss(animated: true, completion: nil)
    })

}
    func setupJobImageView() {
    let header = UIView()
     view.addSubview(header)
     header.anchors(top: self.view.topAnchor, topPad: 120, bottom: nil, bottomPad: 0, left: view.leftAnchor, leftPad: 24, right: view.rightAnchor, rightPad: 24, height: 250, width: 150)
    header.addSubview(image1Button)
    let padding: CGFloat = 16
    image1Button.anchor(top: header.topAnchor, leading: header.leadingAnchor, bottom: header.bottomAnchor, trailing: nil, padding: .init(top: padding, left: padding, bottom: padding, right: 0))
    image1Button.widthAnchor.constraint(equalTo: header.widthAnchor, multiplier: 0.45).isActive = true

    let stackView = UIStackView(arrangedSubviews: [image2Button, image3Button])
    stackView.axis = .vertical
    stackView.distribution = .fillEqually
    stackView.spacing = padding

    header.addSubview(stackView)
    stackView.anchor(top: header.topAnchor, leading: image1Button.trailingAnchor, bottom: header.bottomAnchor, trailing: header.trailingAnchor, padding: .init(top: padding, left: padding, bottom: padding, right: padding))

    }
 }
}

我为疯狂的代码量表示歉意,我不想遗漏与该问题有关的任何信息。我真的希望有人可以看看我,并告诉我他们的想法!

0 个答案:

没有答案