I want to use web image as annotation on Apple Map. In other parts of my app I'm using SDWebImageSwiftUI, so I'd like to use SDWebImage also here. I know there is a SDWebImage plugin for MapKit, but it doesn't look like I can do some operations on image before displaying it. The effect I want to achieve is very close to the Facebook Messenger app:
I store square images of user's in DigitalOcean Space (Amazon S3 compatible bucket), so it has to be loaded from the server. After fetching image I need to make it round and add white background (just like messenger app).
I create annotation view like this:
class MapUserAnnotationView: MKAnnotationView {
static let reuseId = "quickEventUser"
var photo: String?
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
if let ann = annotation as? MapQuickEventUserAnnotation {
self.photo = ann.photo
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForDisplay() {
super.prepareForDisplay()
image = "???"
// Here I need to load image and make edit (probably)
}
}
I tried something like this:
override func prepareForDisplay() {
super.prepareForDisplay()
let img = UIImageView()
img.sd_setImage(with: URL(string: photo ?? ""), completed: {_,_,_,_ in })
image = img.image
}
but it's not working (annotation is not displayed, completed is not called and there is no image load error log)
I know I'm missing something trivial, but I have small experience with MapKit, and I cannot find any source about implementing something similar.
How should I implement it? Is my way of thinking close to correct solution?