缩略图图像不显示在MKMapView注释视图中显示

时间:2015-10-26 19:36:32

标签: swift parse-platform mkmapview mkannotationview

我正在尝试为我的注释视图和从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)")
            }
        })

        }
    }

    }
}

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

1 个答案:

答案 0 :(得分:1)

您需要在分配图像数据后刷新完成块中的视图,或者使用PFImageView而不是UIImageView

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

典型用法

// 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
    }
}