ios swift uiimageview uiimagepickercontroller

ios - Selección de dos imágenes diferentes en el mismo controlador de vista usando imagePickerController en Swift



uiimageview uiimagepickercontroller (1)

Puede configurar una etiqueta diferente para cada botón y, según la etiqueta del botón, actualice una imagen Vista u otra. Solo necesita un UIImagePickerController:

actualización: Xcode 8.3.1 • Swift 3.1

import UIKit class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { @IBOutlet weak var imageView1: UIImageView! @IBOutlet weak var imageView2: UIImageView! var imagePicker = UIImagePickerController() var imagePicked = 0 override func viewDidLoad() { super.viewDidLoad() imagePicker.delegate = self imagePicker.sourceType = .savedPhotosAlbum imagePicker.allowsEditing = false } @IBAction func chooseImage1(_ sender: UIButton) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){ imagePicked = sender.tag present(imagePicker, animated: true) } } func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage if imagePicked == 1 { imageView1.image = pickedImage } else if imagePicked == 2 { imageView2.image = pickedImage } dismiss(animated: true) } func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { dismiss(animated: true) } }

Estoy haciendo una aplicación en la que hay dos UIImageViews. En cada vista de imagen, el usuario debe poder ingresar una imagen diferente. Aquí está el código que tengo hasta ahora.

var imagePicker = UIImagePickerController() @IBAction func chooseImage1(sender: AnyObject) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){ println("Button capture") imagePicker.delegate = self imagePicker.sourceType = .SavedPhotosAlbum imagePicker.allowsEditing = false self.presentViewController(imagePicker, animated: true, completion: nil) } } @IBAction func chooseImage2(sender: AnyObject) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){ println("Button capture") imagePicker2.delegate = self imagePicker2.sourceType = .SavedPhotosAlbum imagePicker2.allowsEditing = false self.presentViewController(imagePicker, animated: true, completion: nil) } } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage chooseImage1.image = pickedImage let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage chooseImage2.image = pickedImage2 dismissViewControllerAnimated(true, completion: nil) } func imagePickerControllerDidCancel(picker: UIImagePickerController) { dismissViewControllerAnimated(true, completion: nil) }

Esto termina eligiendo la misma imagen para cada vista de imagen diferente. Me gustaría poder elegir dos fotos individuales, una para cada vista. Gracias por tu ayuda.