小编典典

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

swift

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

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

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


阅读 284

收藏
2020-07-07

共1个答案

小编典典

您可以为每个按钮设置不同的标签,然后根据按钮的标签更新一个imageView或另一个。您只需要一个UIImagePickerController:

更新: 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 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)
    }
}
2020-07-07