2017-01-19 3 views
0

でUIImagePickerController後にnilである:「サイズ{3024、4032}方位3規模1.000000」が、その後、私はエラーを取得: "致命的なエラー:予想外のオプション値 "をアンラップしている間は、次の行には表示されません。どのように私はちょうどイメージを得ることができますか?iImageの値は、それが言うにプリントアウトされた場合UIImageはスウィフト

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

iImageからエラーが発生していますか?あなたはeventPicがnilではないと確信していますか? – TheAmateurProgrammer

答えて

1

これは、ここでViewControllerの新しいインスタンスを作成するためです。EventPageViewController()です。このデリゲートメソッドがEventPageViewControllerにある場合は、self.eventPic.image = iImageと呼ぶことができます。

+0

現在のViewControllerではありません。アノテーションの情報ボタンがクリックされたときに表示されるViewControllerです – Steve

+0

@ Steveこのデリゲートメソッドはどのように起動されますか? 'EventPageViewController'の一般的なレイアウトは何ですか? –

+0

FirstViewControllerは、ユーザーがボタンをクリックするとカメラを開きます。上には含まれていない別のコード行があり、ピンを使用するメソッドを呼び出しています。アノテーションの情報ボタンをクリックすると、EventPageViewControllerが開きます(ここでは画像を表示します)。 – Steve

0

eventPicは、UIImageViewIBOutletであるようです。したがって、EventPageViewControllerのインスタンスは、ビューのロードとコンセントの接続が完了していません。

EventPageViewControllerにUIImageプロパティが必要です。このことができます

let eventPageViewController = EventPageViewController() 
eventPageViewController.eventImage = iImage 

希望:

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

ので、あなたがデリゲートをピッカーから、あなたは次のようにアクセスすることができます。

関連する問題