为什么我的图像没有出现在UIImage视图中?

时间:2018-02-13 02:33:23

标签: ios swift uiimage

我制作了一个视图控制器来显示一个图像,用户按下checkOutVC中的右键按钮后,图像将显示出来

enter image description here

图片应该出现在ReusableImageDisplayVC这样的

enter image description here

我将图片从checkOutVC发送到ReusableImageDisplayVC,我用于这些视图控制器的代码如下:

class CheckoutTVC: UITableViewController {

 @IBAction func openImageBarButtonDidPressed(_ sender: Any) {

        var displayedImage = UIImage()
        let imagePath = "https://app10.pakubuwono6.com/hris/images/attendance/IMG_20180131_082648_328.jpg"
        let urlImage = URL(string: imagePath)

        NetworkingService.fetchData(url: urlImage!) { (result) in
            self.activityIndicator.startAnimating()

            switch result {
            case .failure(let error) :
                self.activityIndicator.stopAnimating()
                self.showAlert(alertTitle: "Sorry", alertMessage: error.localizedDescription, actionTitle: "Back")
            case .success (let imageData):
                self.activityIndicator.stopAnimating()
                displayedImage = UIImage(data: imageData)!
            }
        }


        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let popup = storyboard.instantiateViewController(withIdentifier: "imageDisplay") as! ReusableImageDisplayVC
        popup.photo = displayedImage


        present(popup, animated: true, completion: nil)

    }
}

,ReusableImageDisplayVC中的代码位于

之下
class ReusableImageDisplayVC: UIViewController {

    @IBOutlet weak var ImagePhoto: UIImageView!
    var photo : UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()

        if photo == nil {
            print("photo is nil")
        } else {
            print("photo is not nil")
        }

        print(photo!)

        ImagePhoto.image = photo!


        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ReusableImageDisplayVC.tapFunction(sender:)))
        tapGesture.numberOfTapsRequired = 1
        ImagePhoto.addGestureRecognizer(tapGesture)
        view.addGestureRecognizer(tapGesture)

    }



    @objc func tapFunction(sender: UITapGestureRecognizer) {
        dismiss(animated: true, completion: nil)
    }


}

正如我们在ReusableImageDisplayVC中看到的那样我试图确保以前VC中的UIImage可用,并且在调试区域中打印它

  

照片不是零,    UIImage:0x6040008bde20,{0,0}

我还分配到了ImageView ImagePhoto.image = photo!

但为什么当我运行该应用时,它不会出现?

但如果我像下面的代码一样在ReusableImageDisplayVC中硬编码下载图像处理,则会出现图像

class ReusableImageDisplayVC: UIViewController {

    @IBOutlet weak var ImagePhoto: UIImageView!
    var photo : UIImage?
    var something : String?

    override func viewDidLoad() {
        super.viewDidLoad()

        let imagePath = "https://app10.pakubuwono6.com/hris/images/attendance/IMG_20180131_082648_328.jpg"
        let urlImage = URL(string: imagePath)

        NetworkingService.fetchData(url: urlImage!) { (result) in

            switch result {
            case .failure(let error) :
                self.showAlert(alertTitle: "Sorry", alertMessage: error.localizedDescription, actionTitle: "Back")
            case .success (let imageData):
               self.ImagePhoto.image = UIImage(data: imageData)!
            }
        }


        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ReusableImageDisplayVC.tapFunction(sender:)))
        tapGesture.numberOfTapsRequired = 1
        ImagePhoto.addGestureRecognizer(tapGesture)
        view.addGestureRecognizer(tapGesture)

    }



    @objc func tapFunction(sender: UITapGestureRecognizer) {
        dismiss(animated: true, completion: nil)
    }


}

这里出了什么问题?

1 个答案:

答案 0 :(得分:3)

当您呈现displayedImage视图控制器时,

ReusableImageDisplayVC尚未设置。这是因为您在异步调用完成之前呈现视图控制器。试试这个:

@IBAction func openImageBarButtonDidPressed(_ sender: Any) {
    NetworkingService.fetchData(url: urlImage!) { [weak self] (result) in
        var displayedImage = UIImage()
        let imagePath = "https://app10.pakubuwono6.com/hris/images/attendance/IMG_20180131_082648_328.jpg"
        let urlImage = URL(string: imagePath)

        self?.activityIndicator.startAnimating()

        switch result {
        case .failure(let error) :
            self?.activityIndicator.stopAnimating()
            self?.showAlert(alertTitle: "Sorry", alertMessage: error.localizedDescription, actionTitle: "Back")
            return //return instead of presenting ReusableImageDisplayVC 
        case .success (let imageData):
            self?.activityIndicator.stopAnimating()
            displayedImage = UIImage(data: imageData)!
        }

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let popup = storyboard.instantiateViewController(withIdentifier: "imageDisplay") as! ReusableImageDisplayVC
        popup.photo = displayedImage

        self?.present(popup, animated: true, completion: nil)
    }
}

(请注意在[weak self]完成处理程序中使用self?returnfetchData