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

Display two images in didFinishPickingImage - iOS Swift 2.2

I have two

UIImageView
s, each with two buttons. One button takes a picture and the other chooses a photo from a library. Both buttons work correctly. But when I choose an image by a button from the first sheet, it is displayed in both
UIImageView
s. My question is how can we display the image in only the 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
    }
}