0
votes

My UIImageView is always blank. When the image view is loaded I have it print the image and image view out. This is the output from the print in first section of code: UIImage: 0x170898010>, {4032, 3024} UIImageView: 0x14bb077e0; frame = (0 64; 375 554); This is the output from the print in second section of code: autoresize = RM+BM; userInteractionEnabled = NO; layer = CALayer: 0x170a31240 It seems to be storing the image but it does not appear.

Here is where the image, which is saved correctly to CloudKit is downloaded and converted:

if let asset = record["Picture"] as? CKAsset,
       let data = NSData(contentsOf: asset.fileURL),
       let image1 = UIImage(data: data as Data)
       {
           let eventPageViewController:EventPageViewController = self.storyboard?.instantiateViewController(withIdentifier: "EventPage") as! EventPageViewController
           eventPageViewController.toPass = image1
           print(image1)
       }

Here is the code for the ViewController that displays the UIImageView:

class EventPageViewController: UIViewController {
    @IBOutlet weak var eventPic: UIImageView!
    var toPass: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()

        eventPic.image = toPass
        print(eventPic)
    }

here is where the eventPageViewController appears:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

        let eventPageViewController:EventPageViewController = storyboard?.instantiateViewController(withIdentifier: "EventPage") as! EventPageViewController

        self.present(eventPageViewController, animated: true, completion: nil)
    }
2
did you check if image is passed as not nil? in your viewDidLoad? try to do initialization of it in viewWillAppearEugene Gordin
I get the same output and result @EugeneZhenyaGordinSteve
what about checking if it's not nil there, have you checked ?Eugene Gordin
its not nil. It prints the same outputSteve
if you try and add some image in your viewDIdLoad from your asset lib there, can you see it?Eugene Gordin

2 Answers

0
votes

From Apple docs about image property:

This property is set to the image you specified at initialization time. If you did not use the init(image:) or init(image:highlightedImage:) method to initialize your image view, the initial value of this property is nil.

https://developer.apple.com/reference/uikit/uiimageview/1621069-image

0
votes

The OP isn't using segues, but I was asked if this would solve things. Here's my description and code that works using a segue.

Baseline:

My app has two view controllers (select and edit) with a segue between them (ShowEditView). That is all I've defined in IB. Everything else is in code.

There is one difference that appears to not be at issue - I use a UIImagePickerController to get the image where the OP pulls it from the assets. This doesn't appear to be at issue because the source VC has the image. So picking it up from here (two VCs with a segue defined in IB, the image in a UIImage instance) here's how I might code things based on the OP's code.

In the source VC:

func imageAttained() {

    // here's where you move the image into image1

    // this is the segue - make sure you've named it in IB
    self.performSegue(withIdentifier: "ShowEventView", sender: self)
}

// this is an override in the source VC and passes the image

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowEventView" {
        if let vc = segue.destination as? EventPageViewController {
            vc.toPass = image1
        }
    }
}

And in the destination VC (EventPageViewController):

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    eventPic.image = toPass
}