我正在使用Mapkit。在leftcalloutaccessory,有一個小圖像,這是從用戶位置採取的。在rightcalloutaccesory中,有一個按鈕可以使另一個viewcontroller變爲segue,因此用戶可以看到大尺寸的圖像。 問題是 - 它是隨機的保存圖像,它將顯示在另一個視圖上。發送挑選的圖像到另一個視圖
class ImageAnnotation: MKPointAnnotation {
var image: UIImage?
}
然後在地圖視圖控制器,它看起來像這樣
var imageAnnotations: [ImageAnnotation] = []
var sendImage: UIImageView!
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? ImageAnnotation else {
return nil
}
let identifier = "MyCustomAnnotation"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
let image = UIImage(named: "advance.png")
let button = UIButton(type: .DetailDisclosure)
button.frame = CGRectMake(0, 0, 30, 30)
button.setImage(image, forState: .Normal)
annotationView?.rightCalloutAccessoryView = button
let detailImage = UIImageView(frame: CGRectMake(0, 0, 50, 50))
detailImage.image = annotation.image
sendImage.image = annotation.image
annotationView?.leftCalloutAccessoryView = detailImage
return annotationView
}
它的工作原理。它在每個註釋中都顯示正確的圖像。但「精美圖像」,可以是每個保存的圖像。
override func viewDidAppear(animated: Bool) {
super.viewWillAppear(animated)
for annotation in imageAnnotations {
mapView.addAnnotation(annotation)
}
gettingData()
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
{
if (view.annotation is MKPointAnnotation) {
print("Clicked annotation ")
if control == view.rightCalloutAccessoryView {
sendImage.image = UIImage(named: "advance.png")
}
}
}
func buttonPressed (sender: UIButton!) {
let currentImage: UIImage = sender.imageForState(.Normal)!
sendImage.image = currentImage
self.performSegueWithIdentifier("ShowLargeImage", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowLargeImage" {
let goToImageViewController = segue.destinationViewController as! ImageViewController
goToImageViewController.newImage = sendImage.image
}
}
在'let currentImage:UIImage = sender.imageForState(.Normal)!中刪除這兩行! sendImage.image = currentImage'在這裏'func buttonPressed(sender:UIButton!)' –
仍然說'意外地發現零,同時展開可選值'。找不到它發生的位置 – jonask
確定在哪一行看到錯誤 –