How to Overlay Image on iOS Card Using Quick

I am trying to figure out how to overlay an image on an iOS card using SWIFT. I created the following code that overlays a green circle on a map using a set of maps. I want to replace the green circle with a rectangular image tOver.png 500 500 I am new to iOS development and fast. While I can not find a quick example or a good resource.

// // ViewController.swift // mapoverlaytest // import UIKit import MapKit class ViewController: UIViewController,MKMapViewDelegate { @IBOutlet weak var mapView: MKMapView! override func viewDidLoad() { super.viewDidLoad() self.mapView.delegate = self; let location = CLLocationCoordinate2D( latitude: 51.50007773, longitude: -0.1246402 ) let span = MKCoordinateSpanMake(0.05, 0.05) let region = MKCoordinateRegion(center: location, span: span) mapView.setRegion(region, animated: true) let annotation = MKPointAnnotation() annotation.setCoordinate(location) annotation.title = "Big Ben" annotation.subtitle = "London" var overlay = MKCircle (centerCoordinate: location, radius: 500) mapView.addOverlay(overlay) mapView.addAnnotation(annotation) } func mapView( mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { if (overlay.isKindOfClass(MKCircle)) { var circleRenderer = MKCircleRenderer(overlay: overlay) circleRenderer.strokeColor = UIColor.greenColor() circleRenderer.fillColor = UIColor( red: 0, green: 1.0, blue: 0, alpha: 0.5) return circleRenderer } return nil } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 
+6
source share
2 answers

As Totem explained, it would be easier to use image annotation instead of overlay if this works for your purposes. However, this may not work depending on what you want to use this image for. The main difference between map overlays and map annotations is annotations that are the same size when the map is scaled (for example, a pin), and overlays change with the size of the map (for example, the marking of a building). If you want your image to be enlarged using a map, it gets a little more complicated.

You will need to create a new subclass of MKOverlayRenderer to draw your image. You must draw the image yourself in the context of the image by subclassing the drawMapRect function (mapRect, zoomScale, inContext). After you create this subclass, you can just replace your own subclass instead of MKCircleRenderer, and you should be good to go.

There is a very good entry on Raywenderlich.com that you should definitely check out. He must go through everything you need to know.

+3
source

Instead of rendererForOverlay you should implement

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 

Inside, create your MKAnnotationView and set its image property before returning it. To learn more about the MKAnnotationView class, https://developer.apple.com/LIBRARY/ios/documentation/MapKit/Reference/MKAnnotationView_Class/index.html

0
source

Source: https://habr.com/ru/post/976784/


All Articles