Search code examples
iosswiftimageuiimagepickercontrollermedia

How to use didFinishPickingMedia with multiple UIImages


I have a view that has four UIButtons. When each button is called it pops up the photo library for the user to choose an image to set as the button's image (think of it as setting a profile image). Here is the code I'm using to do this.

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]!) {        
    let image = info[UIImagePickerControllerOriginalImage] as UIImage
    pickedImage = image        
    p1ImageButton.setImage(pickedImage, forState: UIControlState.Normal)        
    self.dismissViewControllerAnimated(true, completion: nil)        
}

@IBAction func p1PhotoTapped(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){            
        var p1IP = UIImagePickerController()
        p1IP.delegate = self
        p1IP.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;            
        p1IP.allowsEditing = false            
        self.presentViewController(p1IP, animated: true, completion: nil)
    }
}

This works just dandy for the first image. However, because I'm using the IPC delegate, didFinishingPickingMediaWith, I'm not sure how to set the other 3 button images. If I click on the other three buttons(which is assigned their own action), it just keeps calling this same delegate, which then keeps setting the image to the first button.

Is there a way to have multiple delegate methods? Or maybe to have the delegate method check for a certain ImagePickerController? Any help would be great on how to get the other three buttons to set their images correctly. THANKS!


Solution

  • You can't have multiple delegate methods, that's one of the advantages of the block mechanism as opposed to the delegate mechanism.

    A couple of ideas:

    First, keep an instance variable on the UIViewController that tells you which image to set, then just switch based on that:

    enum ButtonToSet {
        case Button1, Button2, Button3, Button4
    }
    var buttonToSet:ButtonToSet?
    
    @IBAction func p1PhotoTapped(sender:AnyObject) {
        buttonToSet = .Button1
        ...
    }
    
    func imagePickerController(picker: UIImagePickerController!,  didFinishPickingMediaWithInfo info: [NSObject : AnyObject]!) {
    
        ...
    
        let image = info[UIImagePickerControllerOriginalImage] as UIImage
        pickedImage = image
    
        if let buttonToSet = buttonToSet {
            switch buttonToSet {
            case .Button1:
                p1ImageButton.setImage(pickedImage, forState: UIControlState.Normal)
            ...
            }
        }
    
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    

    Alternatively, create a UIButton subclass, ProfileImageButton that handles it's own action by popping up the selector itself, then the delegate method is already associated with the correct button.