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

时间:2018-11-13 22:52:46

标签: swift xcode uinavigationcontroller uiimagepickercontroller

晚上好,

我正在尝试创建一个具有三个可选图像视图的VC。 (表示用户可以单击该框,它将从其手机上载图像)

我正在尝试将图像彼此紧紧约束。我希望有一个主盒子,右边有两个较小的盒子。但是当我运行该应用程序时,它崩溃了。 “线程1:信号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)
}

}

1 个答案:

答案 0 :(得分:1)

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

view.addSubview(imageView)
return imageView