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

问题描述 投票:0回答:1

我正在制作一个应用程序,其中有两个 UIImageView。在每个图像视图中,用户需要能够输入不同的图像。这是我到目前为止的代码。

    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)
}

这最终会为每个不同的图像视图选择相同的图像。我希望能够选择两张单独的照片,每个视图一张。谢谢您的帮助。

ios swift uiimageview uiimagepickercontroller
1个回答
16
投票

你只需要一个UIImagePickerController。您可以保留点击视图的引用,当用户完成选择图像时,您只需将选定的视图转换为 UIImageView 并设置其图像属性:

更新:Xcode 11.5 • Swift 5.2 或更高版本

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate ,UINavigationControllerDelegate {
    
    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!

    var imagePicker = UIImagePickerController()
    var selectedView: UIView!
 
    override func viewDidLoad() {
        super.viewDidLoad()
        
        imagePicker.delegate = self
        imagePicker.sourceType = .savedPhotosAlbum
        imagePicker.allowsEditing = false
        
        [imageView1,imageView2].forEach {
            $0?.isUserInteractionEnabled = true
            $0?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseImage)))
        }
    }
    
    @objc func chooseImage(_ gesture: UITapGestureRecognizer) {
        if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
            selectedView = gesture.view
            present(imagePicker, animated: true)
        }
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        (selectedView as? UIImageView)?.image = info[.originalImage] as? UIImage
        dismiss(animated: true)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.