我需要2个不同的imageViews的imagePickerController,但是它不起作用

时间:2019-03-25 10:21:54

标签: swift firebase firebase-realtime-database mobile uiimagepickercontroller

我正在尝试为2个不同的图像视图创建一个imagePickerController,但是它只能选择其中之一。我怎样才能做到两全其美?

这是我的代码:

class UploadViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    @IBOutlet weak var previewImage: UIImageView!
    @IBOutlet weak var postBtn: UIButton!
    @IBOutlet weak var selectBtn: UIButton!

    @IBOutlet weak var imageStep: UIImageView!
    @IBOutlet weak var selectStepsBtn: UIButton!
    @IBOutlet weak var postStepBtn: UIButton!

    var picker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavigationBarItems()
        picker.delegate = self   
    }

    func setupNavigationBarItems() {
        navigationItem.title = "Upload"
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
            self.previewImage.image = image
            selectBtn.isHidden = false
            postStepBtn.isHidden = false
            selectStepsBtn.isHidden = false
            postBtn.isHidden = false
        } 
        picker.dismiss(animated: true, completion: nil)
    }

    @IBAction func selectStepPressed(_ sender: Any) {

        picker.allowsEditing = true
        picker.sourceType = .photoLibrary

        self.present(picker, animated: true, completion: nil)  
    }


    @IBAction func postStepBtn(_ sender: Any) {
        AppDelegate.instance().showActivityIndicator()
        let uid = Auth.auth().currentUser!.uid
        let ref = Database.database().reference()
        let storage = Storage.storage().reference(forURL: "gs://mobile-d9fcd.appspot.com")

        let key = ref.child("subposts").childByAutoId().key
        let imagePlace = storage.child("subposts").child(uid).child("\(key).jpg")

        let data = UIImageJPEGRepresentation(self.imageStep.image!, 0.6)

        let uploadTassk = imagePlace.putData(data!, metadata: nil) { (metadata, error) in
            if error != nil {
                print(error!.localizedDescription)
                AppDelegate.instance().dismissActivityIndicator()
                return
            }

            imagePlace.downloadURL(completion: { (url, error) in
                if let url = url {
                    let feed = ["stepsLabel" : key] as [String : Any]

                    let postFeed = ["\(key)" : feed]

                    ref.child("subposts").updateChildValues(postFeed)
                    AppDelegate.instance().dismissActivityIndicator()

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

    @IBAction func selectPressed(_ sender: Any) {

        picker.allowsEditing = true
        picker.sourceType = .photoLibrary

        self.present(picker, animated: true, completion: nil)
    }

    @IBAction func postPressed(_ sender: Any) {
        AppDelegate.instance().showActivityIndicator()

        let uid = Auth.auth().currentUser!.uid
        let ref = Database.database().reference()
        let storage = Storage.storage().reference(forURL: "gs://mobile-d9fcd.appspot.com")

        let key = ref.child("posts").childByAutoId().key
        let imageRef = storage.child("posts").child(uid).child("\(key).jpg")

        let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)

        let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
            if error != nil {
                print(error!.localizedDescription)
                AppDelegate.instance().dismissActivityIndicator()
                return
            }

            imageRef.downloadURL(completion: { (url, error) in
                if let url = url {
                    let feed = ["userID" : uid,
                                "pathToImage" : url.absoluteString,
                                "likes" : 0,
                                "author" : Auth.auth().currentUser!.displayName!,
                                "postID" : key] as [String : Any]

                    let postFeed = ["\(key)" : feed]

                    ref.child("posts").updateChildValues(postFeed)
                    AppDelegate.instance().dismissActivityIndicator()

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

我尝试为第二个imageview创建另一个imagePickerController,但是它给出了一个错误。

1 个答案:

答案 0 :(得分:0)

您可以使用imagePicker标记属性来区分所选择的图像,如下面的代码所示。

enum ImageNum: Int {
    case one = 1
    case two = 2
}

选择器初始化

var picker = UIImagePickerController()
picker.allowsEditing = true
picker.sourceType = .photoLibrary
picker.view.tag = ImageNum.one.rawValue // or ImageNum.two.rawValue 
self.present(picker, animated: true, completion: nil)

委托:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        if let imageNum = ImageNum.init(rawValue: picker.view.tag) {
            switch imageNum {
            case .one:
                // image 1
                print("one")
            case .two:
                // image 2
                print("two")
            }
        } else {
            // error
        }

    }
}