Đoàn Thanh An Đoàn Thanh An - 3 months ago 15
iOS Question

Display 2 images in didFinishPickingImage - iOS Swift 2.2

I have 2 UIImageView, and 2 buttons. Each button, I have created a action sheet, contains function : take picture and take photo from library. Both button work correctly. But when I choose image from the first button. It shows in both 2 UIImageView. My question is how can we display the image in corresponding imageview.

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
print("Image Selected")
self.dismissViewControllerAnimated(true, completion: nil)

importedImage.image = image
secondPhoto.image = image
}

// MARK: - Action Sheet


@IBAction func showActionSheet1(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false

self.presentViewController(image, animated: true, completion: nil)

})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)


self.presentViewController(actionSheet, animated: true, completion: nil)

}

@IBAction func showActionSheet2(sender: AnyObject) {

let image = UIImagePickerController()
image.delegate = self

let actionSheet = UIAlertController(title: "Action Sheet", message: "Choose Option", preferredStyle: .ActionSheet)
let libButton = UIAlertAction(title: "Select from photo library", style: .Default, handler: { (libButton) -> Void in
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)

})
let cameraButton = UIAlertAction(title: "Take picture", style: .Default, handler: { (cameraButton) -> Void in

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

image.sourceType = UIImagePickerControllerSourceType.Camera
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
})
let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(cancelButton) -> Void in
print("Cancel selected")
})
actionSheet.addAction(libButton)
actionSheet.addAction(cameraButton)
actionSheet.addAction(cancelButton)


self.presentViewController(actionSheet, animated: true, completion: nil)
}

Answer

This is happening because you are setting image in both the ImageView inside didFinishPickingImage, to solve the issue need to maintain some state like which Button is clicked to set Image, for that you can create one Bool instance and assign its value inside the action of Button like this.

var isFromFirst: Bool = false

@IBAction func showActionSheet1(sender: AnyObject) {

    self.isFromFirst = true
    ...//your other code

}

@IBAction func showActionSheet2(sender: AnyObject) {

    self.isFromFirst = false
    ...//your other code

} 

After that check this bool value inside didFinishPickingImage like this.

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {
    print("Image Selected")
    self.dismissViewControllerAnimated(true, completion: nil)
    if (self.isFromFirst) {
        importedImage.image = image
    }
    else {
        secondPhoto.image = image
    }
}