UIImage在Swift中的UIImagePickerController之后是零

时间:2017-01-19 23:02:15

标签: ios swift uiimageview uiimage uiimagepickercontroller

当打印出iImage的值时,它会显示:“ size {3024,4032} orientation 3 scale 1.000000 ”,但后来我收到错误:“致命错误:意外发现在下一行展开“可选值”时为零。我刚刚得到的图像怎么样?

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

2 个答案:

答案 0 :(得分:1)

这是因为您在这里EventPageViewController()创建了一个ViewController的新实例。如果此委托方法位于EventPageViewController,您只需拨打self.eventPic.image = iImage

答案 1 :(得分:0)

看起来eventPicUIImageView IBOutlet。因此,EventPageViewController的实例尚未完成加载视图和连接插座。

您应该在EventPageViewController中拥有UIImage属性。

class EventPageViewController {
  var eventImage: UIImage?
  @IBOutlet var eventPic:UIImageView! //you should have better name like eventImageView

  func viewDidLoad() {
    super.viewDidLoad()
    eventPic?.image = eventImage
    ///rest goes here
  }
}

所以你可以从你的选择代表那里访问:

let eventPageViewController = EventPageViewController()
eventPageViewController.eventImage = iImage

希望这有帮助。