在 swift 中向注释添加图像时遇到问题

Having trouble adding images to an annotation in swift

我已经从 Parses 后端查询了一组 NSData 对象(转换后将成为图像),如下所示:

 var query = PFUser.query()
            query.whereKey("location", nearGeoPoint:geopoint)
            query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]!, error: NSError!) -> Void in

for object in objects {

   var user:PFUser = object as PFUser

     var otherUserImages = [NSData]()

     otherUserImages.append(user["image"] as NSData)

     }
})

我现在有一组 NSData 对象,我想将其用作图像作为地图上的注释。

所以我创建了一个函数:

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

        var view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

                for userImage in self.otherUserImages {

                view.canShowCallout = true

                var dataImage = UIImage(data: userImage)

                var userMapImage = UIImageView(image: dataImage)

                view.leftCalloutAccessoryView = userMapImage

                }
            }

现在这会将图像作为注释放置,但仅在 array[0] 我似乎无法将所有图像都作为注释?

有人可以告诉我我缺少什么吗?

我不清楚你是想将每张图像作为注释处理,还是更喜欢将所有图像作为单个注释处理。

第一种情况,看来你使用的mapView:viewForAnnotation:方法不正确。

我假设您已经创建了 MKAnnotation 对象并将它们添加到您的 MKMapView,因为 mapView:viewForAnnotation: 被正确调用。

现在,您应该考虑的是为每个注释调用 mapView:viewForAnnotation:。然后,对于每个调用,您应该 return 一个 MKPinAnnotationView,并且您应该将单个图像(视图)与它们中的每一个相关联。因此,您需要定义一种机制,以便您可以将 MKAnnotation 对象关联到您的图像,然后只需在 mapView:viewForAnnotation: 中使用该图像。如果您提供有关 MKAnnotation 的更多详细信息,我可以进一步提供帮助。

另一方面,您可以将 "joining" 所有图像合并为一个 UIView 并与单个注释相关联。在这种情况下,您可以这样做:

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

    var view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
    var imageView = UIView(frame:...)
    view.leftCalloutAccessoryView = userMapImage

    for userImage in self.otherUserImages {

            view.canShowCallout = true
            var dataImage = UIImage(data: userImage)
            var userMapImage = UIImageView(image: dataImage)
            imageView.addSubview(userMapImage)
    }
}

老实说,我不确定将所有图像 "join" 合二为一是否真的有意义,但你会知道得更多...

在另外一个币种上,你也应该想想Reusing Annotation Views

编辑:

您需要建立的是您的 annotationCustom 对象与 self.otherUserImages 中的对象之间的关系。我不知道你是怎么做到的。也许您需要在 annotationCustom class 中添加一个新的 属性 来保存表示图像的 NSData ...或者您可能更愿意转换 self.otherUserImages进入 NSDictionary 并将每个注释 name 与相应的图像数据相关联。

完成后,您就可以使用

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

    var view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

    if let myAnnotation = annotation as? annotationCustom {

            view.canShowCallout = true
            var dataImage = myAnnotation.dataForImage
            var userMapImage = UIImageView(image: dataImage)
            view.leftCalloutAccessoryView = userMapImage
    }
    return view;
}

完成上述工作后,您可以考虑使用 dequeue 来提高性能和内存使用率:

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

static NSString *viewIdentifier = @"annotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:viewIdentifier];
if (annotationView == nil) {

    if let myAnnotation = annotation as? annotationCustom {

            view.canShowCallout = true
            var dataImage = myAnnotation.dataForImage
            var userMapImage = UIImageView(image: dataImage)
            view.leftCalloutAccessoryView = userMapImage
    }
    return view;
}

我希望 myAnnotation.dataForImage 位足够清楚...