dinosaysrawr
dinosaysrawr

Reputation: 376

MKAnnotationView scaling image causes calloutView to scale as well

So I have a number of bus stops that are marked as annotations on the map. Instead of the default red bubble or the pin, I wanted to use an image of a bus stop sign, so I successfully changed the image.

In doing so, because the image is 512x512, I decided to scale the MKAnnotationView using transform. Consequently, when I select a bus stop, the callout bubble is now also scaled to the same level, which makes it unreadable.

Is there any way I could just scale the image without scaling the calloutView?

My code:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "stopPin")
    annotationView.canShowCallout = true
    annotationView.image = UIImage(named: "busStop.png")
    let transform = CGAffineTransform(scaleX: 0.25, y: 0.25)
    annotationView.transform = transform
    return annotationView
}

Upvotes: 3

Views: 1454

Answers (1)

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

Use UIGraphics.

let image = UIImage(named: "busStop.png")
let resizedSize = CGSize(width: 100, height: 100)

UIGraphicsBeginImageContext(resizedSize)
image?.draw(in: CGRect(origin: .zero, size: resizedSize))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

annotationView?.image = resizedImage

Upvotes: 3

Related Questions