从推送的视图控制器传回图像并在原始图像上显示图像 VC

Pass image back from a pushed view controller and display image on original VC

我已经完成了几个使用 prepare for segue 以及 protocol/delegate 方法在视图控制器之间传递数据的示例...无济于事,我仍然坚持我的项目并且可以使用一些帮助。

我正在尝试 1) select 来自 VC1 的图像,2) 在 VC2 上编辑图像,以及 3) 将编辑后的图像传回 VC1 并显示它。

我卡在传回和显示部分了。非常感谢所有帮助!

我有一个 HomeViewController (VC1),我从中使用 UIImagePicker 来 select 一个图像,然后 view/edit 我在 didFinishPicking 中推送到堆栈上的另一个视图控制器 (VC2) 上的图像。 ..如下图:

extension HomeViewController : UIImagePickerControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let selectImageandCaptionVC = storyboard?.instantiateViewController(withIdentifier: "SelectImageandCaption") as? SelectImageandCaption

        selectImageandCaptionVC?.image = image

        picker.pushViewController(selectImageandCaptionVC!, animated: true)

    }
  }
}

我在 VC2 上编辑图像,然后我试图传回编辑后的图像,并在 VC1 上显示它。这是我在 VC2 上的代码:

import UIKit

// protocol used for sending data back to HomeViewController
protocol ImageAndCaptionDelegate: class {
func userDidEnterInformation(image: UIImage)
}


class SelectImageandCaption: UIViewController, UIScrollViewDelegate {

weak var delegate: ImageAndCaptionDelegate? = nil

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var imageHeight: NSLayoutConstraint!
@IBOutlet weak var imageWidth: NSLayoutConstraint!


var image : UIImage = UIImage()

override func viewDidLoad() {
    super.viewDidLoad()

    scrollView.delegate = self
    imageView.image = image
}


@IBAction func doneButtonPressed(_ sender: Any) {
    //Code that edits image
     let croppedImage = UIImage(cgImage: croppedCGImage!)

    //Pass image pack to HomeViewController
    delegate?.userDidEnterInformation(image: croppedImage)

    // Attempts to go back to the previous view controller and show croppedImage...

    //let HomeViewController = storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
    //self.navigationController?.pushViewController(HomeViewController!, animated: true)
    //self.navigationController?.popToViewController(HomeViewController!, animated: true)
    //present(HomeViewController!, animated: true, completion: nil)

    self.dismiss(animated: true, completion: nil)

 }
} 

返回 VC1 这是我将裁剪图像传回的代码

class HomeViewController: UIViewController, ImageAndCaptionDelegate {

var image : UIImage = UIImage()
var video : AVAsset?
@IBOutlet weak var step1Image: UIImageView!
@IBOutlet weak var step1ProgressImage: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()

}

func userDidEnterInformation(image: UIImage) {
    self.step1Image.image = image
}

在此先感谢您的帮助!

您需要设置委托

let selectImageandCaptionVC = storyboard?.instantiateViewController(withIdentifier: "SelectImageandCaption") as! SelectImageandCaption
selectImageandCaptionVC.image = image
selectImageandCaptionVC.delegate = self
self.present(selectImageandCaptionVC, animated: true)