swift - 缩略图图像未显示在 MKMapView 注释 View 中

标签 swift parse-platform mkmapview mkannotationview

我正在尝试为我的注释 View 和从 Parse 下载的图像制作左侧标注附件。由于某种原因,与图像关联的图像未显示,左侧标注附件为空白。

这是我当前的代码:

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

     if annotation is ImageAnnotation {

        let reuseId = "image"

        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if anView == nil {
            anView = MKAnnotationView(annotation: ImageAnnotation() as MKAnnotation, reuseIdentifier: reuseId)
            anView!.canShowCallout = true
        } else {

            anView!.annotation = ImageAnnotation() as MKAnnotation
        }

        let cpa = annotation as! ImageAnnotation
        anView!.image = UIImage(named: cpa.imageNameI)

        let button = UIButton(type: UIButtonType.DetailDisclosure) // button with info sign in it

        anView!.rightCalloutAccessoryView = button

        anView!.leftCalloutAccessoryView = UIImageView(frame: CGRectMake(0, 0, 59, 59))


        return anView
    }

    return nil
}



func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {

        if let Annotation =  view.annotation as? ImageAnnotation {

            if let thumbnailImageView = view.leftCalloutAccessoryView as? UIImageView {

func loadImage() {

    let Coordinate = view.annotation?.coordinate

    let lat = Coordinate?.latitude
    let lon = Coordinate?.longitude

    let AnCoordinate = PFGeoPoint(latitude: lat!, longitude: lon!)

    let query = PFQuery(className: "ImageAn")
    query.whereKey("shoutoutlocation", equalTo: AnCoordinate)
    query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]?, error: NSError?) in
        if(error == nil){

            _ = objects as! [PFObject]

            for object in objects! {

                let thumbNail = object["image"] as! PFFile

                thumbNail.getDataInBackgroundWithBlock({
                    (imageData: NSData?, error: NSError?) -> Void in
                    if (error == nil) {

                        let Image = UIImage(data:imageData!)

                        thumbnailImageView.image = Image

                        }
                    })
                }

            }else{
                print("Error in retrieving \(error)")
            }
        })

        }
    }

    }
}

谁能告诉我我做错了什么? 谢谢

最佳答案

您需要在分配图像数据后在完成 block 中刷新 View ,或者您可以使用 PFImageView 而不是 UIImageView

PFImageView 包含在 ParseUI 框架中,并在下载文件时自动处理显示占位符图像,然后在准备就绪时更新 View 。

典型用法

// Set placeholder image
thumbnailImageView.image = UIImage(named: "thumbnail_placeholder")

// Set remote image (PFFile)
thumbnailImageView.file = object["image"] as! PFFile

// Once the download completes, the remote image will be displayed
thumbnailImageView.loadInBackground { (image: UIImage?, error: NSError?) -> Void in
    if (error != nil) {
        // Log details of the failure
        println("Error: \(error!) \(error!.userInfo!)")
    } else {
        // profile picture loaded
    }
}

关于swift - 缩略图图像未显示在 MKMapView 注释 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33354136/

相关文章:

iphone - 在MKMapView中找到地址

ios - Struct 中的自初始化错误

objective-c - swift 3 : Stop it from converting [NSString] to [String]

swift - 为用户注册添加 google drive 集成

android - 使用 Volley 发布 JsonObject

ios - 在不使 MKOverlay 变暗的情况下使 MKMapView 的背景颜色变暗

ios - 位置管理器 :didUpdateLocations never called

android - 如何使用 parse.com android 获取当前用户信息

ios - parse.com 删除对象,顺便说一下,删除与之相关的对象

mapkit - 如何在iOS map 中突出显示国家