将捕获的图像/图库图像从一个ViewController传递到另一个,可以通过以下步骤实现:
import UIKit
class ViewControllerA: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
// 捕获图像按钮的动作
@IBAction func captureImage(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
}
// 选择图库图像按钮的动作
@IBAction func chooseImage(_ sender: UIButton) {
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
// 图像捕获/选择完成后的回调方法
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
dismiss(animated: true, completion: nil)
// 将图像传递给ViewController B
let viewControllerB = ViewControllerB()
viewControllerB.image = image
navigationController?.pushViewController(viewControllerB, animated: true)
}
}
// 图像捕获/选择取消后的回调方法
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
import UIKit
class ViewControllerB: UIViewController {
var image: UIImage?
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if let image = image {
imageView.image = image
}
}
}
这样,当你在ViewController A中捕获或选择图像后,它将被传递到ViewController B,并在ViewController B中显示出来。
对于云计算领域的相关知识,可以参考腾讯云的文档和产品介绍。以下是一些相关的腾讯云产品和链接:
请注意,以上链接仅供参考,具体的产品和服务选择应根据实际需求和情况进行评估和决策。
领取专属 10元无门槛券
手把手带您无忧上云