繁体   English   中英

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

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

我已经看过几个使用prese for segue以及协议/委托方法在视图控制器之间传递数据的示例,但无济于事,我仍然对我的项目感到困惑,可以使用一些帮助。

我正在尝试1)从VC1中选择一个图像,2)在VC2上编辑该图像,以及3)将编辑后的图像传递回VC1并显示它。

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

我有一个HomeViewController(VC1),可以使用UIImagePicker从中选择一个图像,然后在另一个视图控制器(VC2)中查看/编辑该图像,然后将它推入didFinishPicking ...中的堆栈中,如下所示:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM