简体   繁体   中英

Swift UIImageView is always empty

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)
    }

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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