gpt4 book ai didi

ios - 图像不是从一个 VC 到另一个 VC

转载 作者:行者123 更新时间:2023-11-28 14:01:02 27 4
gpt4 key购买 nike

图像不是从一个 VC 到另一个 VC。

问题是将它显示到主 UIViewController 中的 ImageView 。我已将所有内容正确连接到 Storyboard中。

点击here查看我的 Storyboard布局。

请注意我的一些不必要的代码已从两个 ViewController 类中删除。

这是我的第一个 UIViewController:

import UIKit

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

var imagePicker: UIImagePickerController!

@IBAction func editPhoto(_ sender: UIButton) {
let ViewController2 = storyboard?.instantiateViewController(withIdentifier: "AddNewEaterySegue") as! ViewController2
ViewController2.imageForEdit = imageView.image!
print(imageView)
print(imageView.image!)
navigationController?.pushViewController(ViewController2, animated: true)
}

@IBOutlet weak var imageView: UIImageView! {
didSet {
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
}
}

}

这是我的第二个 UIViewController:

import UIKit

class ViewController2: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

var filter : CIFilter!
var imageForEdit = UIImage()
var imagePicker: UIImagePickerController!

@IBOutlet weak var select: UISegmentedControl!
@IBOutlet weak var imageLabel: UIImageView!
@IBOutlet weak var textField: UITextField!

@IBAction func saveButtonPressed(_ sender: UIImage) {
if textField.text == "" {
print("Не все поля заполнены")
} else {


}
performSegue(withIdentifier: "unwindSegueFromViewController", sender: sender)
}

override func viewDidLoad() {
super.viewDidLoad()
imageLabel.image = imageForEdit
print(imageLabel)
// Do any additional setup after loading the view.
}


@IBAction func tappedEnter(_ sender: Any) {

if textField.text?.isEmpty ?? true {
return
} else {
if let texttxt = textField.text {

let data = texttxt.data(using: .ascii, allowLossyConversion: false)

if select.selectedSegmentIndex == 0
{
filter = CIFilter(name: "CICode128BarcodeGenerator")
} else {
filter = CIFilter(name: "CIQRCodeGenerator")
}

filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 5, y: 5)
let image = UIImage(ciImage: filter.outputImage!.transformed(by: transform))
imageLabel.image = image
}
}
}
}

最佳答案

当您推送到 ViewController2 时,ViewController 中的 imageView 是空的。

您究竟在哪里为 imageView 设置图像?

在您的 Storyboard中,ViewController 中的 imageView 是空的,并且在 ViewController 中 imageView 的 didSet 方法中您还没有为 imageView 设置任何图像。

在 ViewController 中试试这个:

 @IBOutlet weak var imageView: UIImageView! {
didSet {

//Add a test image to your project.
imageView.image = UIImage(named: "YOUR_TEST_IMAGE_NAME.jpeg")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true

}
}

编辑: 好吧,我想我对你的问题并不清楚。因此,为了将图像从“ViewController2”发送回“ViewController”,您可以使用多种方法,但我会采用的方法是使用 Protocols。

我以一种通用的方式完成了它,但您可以应用它来将任何类型的数据从一个 View Controller 发送回前一个 View Controller 。

在 ViewController 中试试这个:

import UIKit

protocol ImageViewProtocol{
func sendImageToViewController(theImage: UIImage)
}

class ViewController: UIViewController, ImageViewProtocol {

func sendImageToViewController(theImage: UIImage) {
imageView.image = theImage
}

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

let viewcontroller2 = storyboard?.instantiateViewController(withIdentifier: "viewcontroller2") as! ViewController2
viewcontroller2.delegate = self
self.navigationController?.pushViewController(viewcontroller2, animated: true)

}

}

在 ViewController2 中试试这个:

import UIKit

class ViewController2: UIViewController {

var delegate: ImageViewProtocol!

@IBOutlet weak var barcodeImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

barcodeImageView.image = UIImage(named: "test_image.jpg")

}

@IBAction func saveButtonAction(_ sender: Any) {
delegate.sendImageToViewController(theImage: barcodeImageView.image!)
self.navigationController?.popViewController(animated: true)
}

}

关于ios - 图像不是从一个 VC 到另一个 VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53546915/

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