您可以为每个按钮设置不同的标签,然后根据按钮的标签更新一个imageView或另一个。您只需要一个UIImagePickerController:
更新: Xpre 8.3.1•Swift 3.1
import UIKitclass 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 chooseImage(_ 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) }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)