Ios Swift中UIImagePickerController之后的UIImage为零
当iImage的值被打印出来时,它会显示:“大小{30244032}方向3比例1.000000”,但随后我得到了错误:“致命错误:在下一行展开可选值时意外发现了nil”。我刚刚得到的图像怎么可能是零Ios Swift中UIImagePickerController之后的UIImage为零,ios,swift,uiimageview,uiimage,uiimagepickercontroller,Ios,Swift,Uiimageview,Uiimage,Uiimagepickercontroller,当iImage的值被打印出来时,它会显示:“大小{30244032}方向3比例1.000000”,但随后我得到了错误:“致命错误:在下一行展开可选值时意外发现了nil”。我刚刚得到的图像怎么可能是零 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let iImage = info[UIImageP
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
,它看起来像eventPic
是一个UIImageView
作为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
希望这有帮助。您确定错误来自iImage吗?您确定eventPic不是nil吗?它不是当前的ViewController。它是注释上的“信息”按钮打开时显示的ViewControllerclicked@Steve那么这个委托方法是如何被激发的呢?
EventPageViewController
的总体布局是什么?当用户单击按钮时,FirstViewController会打开相机。上面没有包含另一行代码,它调用一个执行pin的方法。单击批注上的“信息”按钮时,它会打开EventPageViewController(我希望图片显示在这里)。所有这些都会保存到cloudkit,以便其他用户可以下载PIN。我需要每个pin都有自己的EventPageViewController实例,以便每个pin都有自己不同的图片@普拉蒂克Patel@Steve因此,您需要将图像传回FirstViewController
,然后使用segue或任何必须打开EventPageViewController
的机制将其传递。