在Swift中使用imagePickerController在同一视图控制器中选择两个不同的图像

在Swift中使用imagePickerController在同一视图控制器中选择两个不同的图像,第1张

在Swift中使用imagePickerController在同一视图控制器中选择两个不同的图像

您可以为每个按钮设置不同的标签,然后根据按钮的标签更新一个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)    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5126482.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存