Multiple Image Pickers in one Controller

前端 未结 1 1956
萌比男神i
萌比男神i 2021-01-27 04:24

I\'m working on an onboarding flow for my app. I\'m wanting to allow users to pick an avatar, display that avatar in an image view, and then select a background photo and displa

1条回答
  •  猫巷女王i
    2021-01-27 05:16

    The delegate function imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) also returns the UIImagePickerController that finished picking an image.

    So you can use an if statement to check if the picker that finished is pickerOne or pickerTwo. Then you implement different behaviour according to the result of that.

    Maybe set the pickers to nil after they have finished to clean up some memory.

    class multiPickerVC : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    
        var pickerOne : UIImagePickerController?
        var pickerTwo : UIImagePickerController?
    
    
        override func viewDidLoad() {
            //
        }
    
    
        @IBAction func getBackground(sender: AnyObject) {
            pickerTwo = UIImagePickerController()
            pickerTwo!.delegate = self
            pickerTwo!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            pickerTwo!.allowsEditing = true
            self.presentViewController(pickerTwo!, animated: true, completion: nil)
        }
    
        @IBAction func selectAvatar(sender: AnyObject) {
            pickerOne = UIImagePickerController()
            pickerOne!.delegate = self
            pickerOne!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            pickerOne!.allowsEditing = true
            self.presentViewController(pickerOne!, animated: true, completion: nil)
        }
    
    
        func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    
            if picker == pickerOne {
    
                // set image for button
                let image = info[UIImagePickerControllerOriginalImage] as? UIImage
                self.addAvatar.setImage(image, forState: .Normal)
    
            } else if picker == pickerTwo {
    
                // set image for button
                let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    
                self.headerImage.image = image
            }
    
            self.dismissViewControllerAnimated(true, completion: nil)
    
        }
    }
    

    0 讨论(0)
提交回复
热议问题