EventPageViewController

EventPageViewController

当iImage的值被打印出来时,它会说:“size{3024,4032}orientation 3 scale 1.000000”,但随后我在下一行得到错误:“fatal error:unexpected found nil while unwrapping an Optional value”。我刚刚得到的图像怎么可能是零?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let iImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        print(iImage)
        createEvent(iImage)//where it is saved to cloudkit with location and title
        EventPageViewController().eventPic.image = iImage
        self.dismiss(animated: true, completion: nil);
    }

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

func loadEvent(_ completion: @escaping (_ error:NSError?, _ records:[CKRecord]?) -> Void)
    {
        let date = NSDate(timeInterval: -60.0 * 180, since: NSDate() as Date)
        let predicate = NSPredicate(format: "creationDate > %@", date)
        let query = CKQuery(recordType: "Event", predicate: predicate)
        CKContainer.default().publicCloudDatabase.perform(query, inZoneWith: nil){
            (records, error) in
            if error != nil {
                print("error fetching events: \(error)")
                completion(error as NSError?, nil)
            } else {
                print("found events")
                completion(nil, records)
                guard let records = records else {
                    return
                }
                for record in records
                {
                    self.drawEvents(record["LocationF"] as! CLLocation, title1: record["StringF"] as! String)

                    if let asset = record["Picture"] as? CKAsset,
                        let data = NSData(contentsOf: asset.fileURL),
                        let image1 = UIImage(data: data as Data)
                    {
                        //EventPageViewController().eventPic.image = image1
                    }
                }
            }
        }
    }

func drawEvents(_ loc: CLLocation, title1: String)
    {
        mapView.delegate = self
        let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
        let lat: CLLocationDegrees = center.latitude
        let long: CLLocationDegrees = center.longitude
        self.pointAnnotation1 = MKPointAnnotation()
        self.pointAnnotation1.title = title1
        self.pointAnnotation1.subtitle = "Event"
        self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)

        self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
        self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
    }

最佳答案

这是因为您正在此处创建ViewController的新实例EventPageViewController()。如果此委托方法位于EventPageViewController中,则只需调用self.eventPic.image = iImage

09-07 14:20