ios - 在 Swift 中的 UIImagePickerController 之后,UIImage 为 nil

标签 ios swift uiimageview uiimage uiimagepickercontroller

当打印出 iImage 的值时,它说:“ size {3024, 4032} orientation 3 scale 1.000000”,但随后我收到错误:“fatal error: unexpectedly found在下一行展开可选值时为零。我刚刚得到的图像怎么会是nil?

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

最佳答案

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

关于ios - 在 Swift 中的 UIImagePickerController 之后,UIImage 为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753316/

相关文章:

objective-c - 如何使 UIView 可点击

ios - 提取不同格式的JSON数据

ios - 将 CoreData 保存到 iCloud (Swift)

datetime - ios 上日期时间格式的语言与语言环境

ios - 如何对拼接图像进行动画处理?

ios - Xcode UI 测试 - 与测试管理器服务失去连接

带有 Tabbar 的 iOS7 自动布局与 UIScrollView 和可缩放的 UIImageView 不工作

ios - 如何在 MFMailcomposer View 中显示默认导航栏而不是 ios 中的自定义导航栏

ios - Swift 3 - 检测 UITextView 中的新行

ios - 如何让表格 View 在 ImageView 上方滚动?