【问题标题】:Having trouble adding images to an annotation in swift无法快速将图像添加到注释中
【发布时间】:2015-01-15 22:21:32
【问题描述】:

我已经从 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] 我似乎无法将所有图像作为注释?

谁能告诉我我错过了什么?

【问题讨论】:

    标签: arrays swift parse-platform


    【解决方案1】:

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

    在第一种情况下,您似乎错误地使用了mapView:viewForAnnotation:方法。

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

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

    另一方面,您可以考虑将所有图像“加入”到单个 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)
        }
    }
    

    老实说,我不确定将所有图像“合并”成一个是否真的有意义,但你会知道的更清楚......

    在另一个硬币上,你也应该考虑Reusing Annotation Views

    编辑:

    您需要建立annotationCustom 对象和self.otherUserImages 中的对象之间的关系。我不知道你是怎么做到的。也许您需要在 annotationCustom 类中添加一个新属性来保存代表图像的 NSData...对应的图像数据。

    一旦你这样做了,那么你就可以使用

    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 位足够清晰...

    【讨论】:

    • 您好,感谢您的回复,我正在尝试您的第一个选项,将我所有的图像作为单个注释处理。我创建了一个带有初始化程序的注释类 - 坐标、标题、副标题。然后用代码创建注释:var otherAnnotation = annotationCustom(coordinate: newFinalLocation, title: name, subtitle: gender) self.myMap.addAnnotation(otherAnnotation) 抱歉,如果你能再帮忙的话,这太乱了?
    • 是的,这很有帮助,我觉得我快到了。我在 annotationCustom 类中创建了 NSData 对象来保存数据,如下所示:var dataImage = [NSData]() 然后我试图从我的 MapViewController 中附加它:var annotationData = annotationCustom().dataImage annotationData.append(user["image"] as NSData) 尽管我收到错误消息:“缺少参数的参数'coordinate' in call”我如何解决这个问题,因为我只想将我的数据添加到 annotationCustom 数据对象而不是初始化实际的类?感谢您迄今为止的所有帮助。
    • 我现在已经设法解决了这个问题,我做错了一些非常简单的事情。感谢您的所有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多