缩略图图像未显示在 MKMapView 注释视图中
Thumbnail Image not show showing up in MKMapView Annotation 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)")
}
})
}
}
}
}
谁能告诉我我做错了什么?
谢谢
您需要在分配图像数据后刷新完成块中的视图,或者您可以使用 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 下载的图像制作左侧标注附件。由于某种原因,与图像关联的图像未显示,左侧标注附件为空白。
这是我当前的代码:
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
}
}