swift - 如何在 MKAnnotation CalloutAccessoryView 中显示所选图像,swift 2.2

标签 swift realm mkannotation calloutview

如何设置我的 map MKAnnotation CalloutAccessoryView 以显示从我的 Realm 数据库映射的选定图像(两侧都有图标和按钮,作为左侧和右侧的 calloutAccessory?)。我还需要它来识别我的 MapViewController 上其他函数的类/成员?数据和左右标注配件的映射工作正常,但我不知道如何添加所选图像,因此它在标注上显示为大型海报风格的图像(带有小图标和按钮两侧)。

图像是SpecimenAnnotation.objectPoster

SpecimenAnnotation(可以很好地映射数据)和“objectPoster”值是从类 Specimen 中选择(并映射)的注释,包含在我的文件 Specimen.swift 中,如下所示;

 class Specimen: Object{
  dynamic var name = ""
 dynamic var objectPoster = ""
 dynamic var latitude = 0.0
 dynamic var longitude = 0.0
 dynamic var created = NSDate()
 dynamic var category: Category!
}

在我的 MapViewController 中,在“annotationView.detailCalloutAccessoryView = UIImageView(image: SpecimenAnnotation.objectPoster)”行上,我收到红色警报错误“实例成员“objectPoster”不能在“样本注释”类型上使用

我是初学者。非常感谢您的建议。有什么想法吗?

以下是有关此数据和错误的代码块:

       // Create annotations for each one
    for specimen in specimens { // 3
        let coord = CLLocationCoordinate2D(latitude: specimen.latitude, longitude: specimen.longitude);

        let specimenAnnotation = SpecimenAnnotation(coordinate: coord,  poster: specimen.objectPoster, subtitle: specimen.category.name, specimen: specimen)

        mapView.addAnnotation(specimenAnnotation) // 4
    }
   }

}

 //MARK: - MKMapview Delegate
 extension MapViewController: MKMapViewDelegate {

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    guard let subtitle = annotation.subtitle! else { return nil }

    if (annotation is SpecimenAnnotation) {
        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(subtitle) {
            return annotationView
        } else {

            let currentAnnotation = annotation as! SpecimenAnnotation

            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: subtitle)
            annotationView.image = UIImage(named:"IconStudent")
            annotationView.enabled = true
            annotationView.canShowCallout = true                      

            // ERROR message here> 
            annotationView.detailCalloutAccessoryView = UIImageView(image: SpecimenAnnotation.objectPoster)

            // right accessory view
            let infoButton = UIButton(type: UIButtonType.InfoDark)
            // Left accessory view
            let image = UIImage(named: "button50")
            let specimenButton = UIButton(type: .Custom)
            specimenButton.frame = CGRectMake(0, 0, 30, 30)
            specimenButton.setImage(image, forState: .Normal)
            annotationView.rightCalloutAccessoryView = infoButton
            annotationView.leftCalloutAccessoryView = specimenButton

            if currentAnnotation.title == "Empty" {
                annotationView.draggable = true
            }              
            return annotationView
        }
    }
    return nil

}

最佳答案

我通过将值拉入 currentAnnotation 并将其添加到 DetailCalloutAccessoryView 上的 UIImageView 中解决了这个问题

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    guard let subtitle = annotation.subtitle! else { return nil }

    if (annotation is SpecimenAnnotation) {
        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(subtitle) {
            return annotationView
        } else {

            let currentAnnotation = annotation as! SpecimenAnnotation

            let currentImage = currentAnnotation.objectPoster!


            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: subtitle)
            }

            annotationView.enabled = true
            annotationView.canShowCallout = true

            // right accessory view
            let calloutInfoButton = UIButton(type: UIButtonType.InfoDark)

            // Left accessory view
            let calloutLeftImage = UIImage(named: "button50p")
            let calloutLeftButton = UIButton(type: .Custom)
            calloutLeftButton.frame = CGRectMake(0, 0, 30, 30)
            calloutLeftButton.setImage(calloutLeftImage, forState: .Normal)

            //show Image on callout Accessory

            let url = NSURL(string: currentImage)
            let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
            //annotationView.image = UIImage(data: data!)
            let calloutImage = UIImage(data:data!)

            annotationView.detailCalloutAccessoryView = UIImageView(image: calloutImage)

            annotationView.rightCalloutAccessoryView = calloutInfoButton
            annotationView.leftCalloutAccessoryView = calloutLeftButton

            if currentAnnotation.title == "Empty" {
                annotationView.draggable = true
            }
            return annotationView
        }
    }
    return nil

}

关于swift - 如何在 MKAnnotation CalloutAccessoryView 中显示所选图像,swift 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39095295/

相关文章:

swift - 我应该使用 Delegation 还是 DataStore 在 Clean Swift 架构中向后传递数据?

Swift 在十进制格式中失去精度

swift - 更改方向时更改 collectionViewCell 标签字体大小

android - 如何使用从后台线程加载的 RealmResult 并在 Android 主线程中使用它

java - 'getInstance(io.realm.RealmConfiguration )' in ' io.realm.Realm' cannot be applied to '(android.content.Context)' 错误

ios - 在 MKMapView 中标记位置

iphone - MKAnnotation,简单示例

ios - MKAnnotation 需要在长按可拖动之前单击

ios - 按钮点击时的 UIDatePicker

swift - 在 Swift 中使 Realm Loop 通用且可重用