缩略图图像不显示在MKMapView注释视图
问题描述:
我想为我的注释视图和从解析下载的图像制作左侧标注附件。出于某种原因,与图像相关的图像没有显示出来,而左侧的标注配件是空白的。缩略图图像不显示在MKMapView注释视图
这里是我当前的代码:
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)")
}
})
}
}
}
}
谁能告诉我什么,我做错了什么? 谢谢
答
您需要在分配图像数据后刷新完成块中的视图,或者可以使用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
}
}
请问占位符图像名称仅仅是类的名字? –
林有点困惑。 –
占位符图像名称是您决定添加的任何内容。您应该为应用的图片资源添加某种默认图片,然后在没有网络连接从Parse下载数据时显示该图片资源。例如,你可以有一个像问号一样简单的东西https://www.adagio.com/images5/question_mark.jpg – Russell