注释正在MapKit中刷新



我正在Swift中开发MapKit,但遇到了一些问题。我在MapKit上显示带图像的注释,它在100ms内显示一个图像,然后显示真实图像。

extension MapVC: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin")
if annView == nil {
annView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annView?.canShowCallout = true
}
let manager = SDWebImageManager.shared
let imagePath = (annotation as! PointAnnotation).user?.photo
manager.loadImage(with: URL(string: imagePath!), options: .continueInBackground, progress: nil) { (image, data, error, cacheType, finished, url) in
if image != nil {
annView?.image = image?.resizeImageWith(newSize: CGSize(width: 16, height: 16))?.circleMasked
}
}
return annView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let annotation = view.annotation
let user = (annotation as! PointAnnotation).user
let me = mainStore.state.profile
if user === me {
return
}
let vc = ChatVC.storyBoardInstance
vc.user = user
self.navigationController?.pushViewController(vc, animated: true)
}
}

请帮我解决这个问题

loadImage之前尝试以下代码。

annView?.image = nil
manager.loadImage(with:....

已编辑

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin")
if annView == nil {
annView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annView?.canShowCallout = true
let manager = SDWebImageManager.shared
let imagePath = (annotation as! PointAnnotation).user?.photo
manager.loadImage(with: URL(string: imagePath!), options: .continueInBackground, progress: nil) { (image, data, error, cacheType, finished, url) in
if image != nil {
annView?.image = image?.resizeImageWith(newSize: CGSize(width: 16, height: 16))?.circleMasked
}
}
} else {
annView?.annotation = annotation
}
return annView
}

最新更新