如何在单个视图中将多个可选图像添加到 UIImagePickerControllerDelegate?

How to add multiple selectable images to a UIImagePickerControllerDelegate in a single view?

晚上好,

我正在尝试创建具有三个可选图像视图的 VC。 (这意味着用户可以点击该框,它会从他们的 phone 上传一张图片)

我试图限制彼此相邻的图像。我希望有主箱,右边有两个较小的箱子。但是当我 运行 应用程序时,它崩溃了。 "thread 1: Signal SIGABRT"

我会在下面留下我的代码。感谢您的帮助!

     setupJobImageView()
    setupJobImageView2()
   setupJobImageView3()
}

func setupJobImageView() {
     JobImageView.anchors(top: self.view.topAnchor, topPad: 20, bottom: nil, bottomPad: 0, left: view.leftAnchor, leftPad: 24, right: nil, rightPad: 0, height: 50, width: 0)
}
lazy var JobImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "picture")
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFill

    imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectJobImageView)))
    imageView.isUserInteractionEnabled = true
    return imageView
}()
func setupJobImageView2() {
     JobImageView2.anchors(top: self.view.topAnchor, topPad: 20, bottom: nil, bottomPad: 0, left: JobImageView.rightAnchor, leftPad: 24, right: nil, rightPad: 0, height: 30, width: 0)

}
lazy var JobImageView2: UIImageView = {
    let imageView2 = UIImageView()
    imageView2.image = UIImage(named: "add")
    imageView2.translatesAutoresizingMaskIntoConstraints = false
    imageView2.contentMode = .scaleAspectFill
    imageView2.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectJobImageView2)))
    imageView2.isUserInteractionEnabled = true
    return imageView2
}()
func setupJobImageView3() {
     JobImageView3.anchors(top: JobImageView2.bottomAnchor, topPad: 20, bottom: nil, bottomPad: 0, left: JobImageView.rightAnchor, leftPad: 24, right: nil, rightPad: 0, height: 30, width: 0)
}
lazy var JobImageView3: UIImageView = {
    let imageView3 = UIImageView()
    imageView3.image = UIImage(named: "add")
    imageView3.translatesAutoresizingMaskIntoConstraints = false
    imageView3.contentMode = .scaleAspectFill
    imageView3.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectJobImageView3)))
    imageView3.isUserInteractionEnabled = true
    return imageView3
}()



import UIKit

 extension newPostViewController: UIImagePickerControllerDelegate,  UINavigationControllerDelegate {


@objc func handleSelectJobImageView() {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.allowsEditing = true

    present(picker, animated: true, completion: nil)
}
@objc func handleSelectJobImageView2() {
    let picker2 = UIImagePickerController()

    picker2.delegate = self
    picker2.allowsEditing = true

    present(picker2, animated: true, completion: nil)
}
@objc func handleSelectJobImageView3() {
    let picker3 = UIImagePickerController()

    picker3.delegate = self
    picker3.allowsEditing = true

    present(picker3, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    var selectedImageFromPicker: UIImage?

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        selectedImageFromPicker = editedImage
    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {

        selectedImageFromPicker = originalImage
    }

    if let selectedImage = selectedImageFromPicker {
        JobImageView.image = selectedImage
    }
    if let selectedImage = selectedImageFromPicker {
        JobImageView2.image = selectedImage
    }
    if let selectedImage = selectedImageFromPicker {
        JobImageView3.image = selectedImage
    }

    dismiss(animated: true, completion: nil)

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    print("canceled picker")
    dismiss(animated: true, completion: nil)
}

}

您可能需要在设置约束之前添加要查看的 imageView,因此对所有这些都执行此操作

view.addSubview(imageView)
return imageView