gpt4 book ai didi

ios - 捕获两个图像并将其显示到两个 UIImageView

转载 作者:行者123 更新时间:2023-11-29 05:45:27 24 4
gpt4 key购买 nike

我计划拍摄两张个人资料照片,两张照片在同一个 View Controller 上都有自己单独的按钮和 ImageView 。

为此,我使用 Swift 版本 4。

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var profileA: UIImageView!
@IBOutlet weak var profileB: UIImageView!

var profileAPicker: UIImagePickerController!
var profileBPicker: UIImagePickerController!

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

profileAPicker.dismiss(animated: true, completion: nil)
profileA.image = info[.originalImage] as? UIImage

profileBPicker.dismiss(animated: true, completion: nil)
profileB.image = info[.originalImage] as? UIImage
}

@IBAction func takeProfileA(_ sender: Any) {

profileAPicker = UIImagePickerController()
profileAPicker.delegate = self
profileAPicker.sourceType = .camera

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

@IBAction func takeProfileB(_ sender: Any) {

profileBPicker = UIImagePickerController()
profileBPicker.delegate = self
profileBPicker.sourceType = .camera

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

}

当我为 profileA 拍照时它可以工作,但当我为 profileB 拍照时它就停止了。

最佳答案

你的方法是不正确的。您的代码对两个隐式解包的选项调用 dismiss ,而不考虑属性是否已设置。

您不需要任何图像选择器属性。只需跟踪要更新的 ImageView 即可。

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var profileA: UIImageView!
@IBOutlet weak var profileB: UIImageView!

var currentImageView: UIImageView?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)

currentImageView?.image = info[.originalImage] as? UIImage
}

@IBAction func takeProfileA(_ sender: Any) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
currentImageView = profileA

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

@IBAction func takeProfileB(_ sender: Any) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
currentImageView = profileB

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

}

关于ios - 捕获两个图像并将其显示到两个 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56187853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com