OGeek|极客世界-中国程序员成长平台

标题: ios - MKOverlayRenderer 拉伸(stretch)图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:28
标题: ios - MKOverlayRenderer 拉伸(stretch)图像

我想在 map 叠加层中放置一张图片,该叠加层将与 map 一起缩放。使用下面的代码,图像会出现在 map View 中,但会被拉伸(stretch)以适应 View 。如何在叠加层内保持图像的原始纵横比?

MapOverlay.swift

import UIKit
import MapKit

class MapOverlay: NSObject, MKOverlay {

    var coordinate: CLLocationCoordinate2D
    var boundingMapRect: MKMapRect

    init(coord: CLLocationCoordinate2D, rect: MKMapRect) {
        self.coordinate = coord
        self.boundingMapRect = rect
    }
}

MapOverlayView.swift

import UIKit
import MapKit

class MapOverlayView: MKOverlayRenderer {

    var overlayImage: UIImage

    init(overlay: MKOverlay, overlayImage:UIImage) {
        self.overlayImage = overlayImage
        super.init(overlay: overlay)
    }

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        let mapImage = overlayImage.cgImage
        let mapRect = rect(for: overlay.boundingMapRect)
        context.scaleBy(x: 1.0, y: -1.0)
        context.translateBy(x: 0.0, y: -mapRect.size.height)
        context.draw(mapImage!, in: mapRect)
    }
}

ViewController.swift

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var mapview: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapview.delegate = self

        let location = CLLocationCoordinate2D(latitude: 47.6062, longitude: -122.3320)
        let span = MKCoordinateSpanMake(2.0, 2.0)
        let region = MKCoordinateRegion(center: location, span: span)
        mapview.setRegion(region, animated: true)

        let rec = mapview.visibleMapRect
        let overlay = MapOverlay(coord: location, rect: rec)
        mapview.add(overlay)
    }

}

extension ViewController: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

        if overlay is MapOverlay {
            let logo = UIImage(named: "swift")
            let overlayView = MapOverlayView(overlay: overlay, overlayImage: logo)
            return overlayView
        } else {
            return MKPolylineRenderer()
        }
    }
}



Best Answer-推荐答案


在你的 ViewController.swift 中,在 viewDidLoad() 中,

let rec = mapview.visibleMapRect

改变这个,rec应该和使用的图片大小完全一样。

            let location = //Give your location here in CLLocationCoordinate2D
            //1. Show direction Using Overlays
            let span = MKCoordinateSpanMake(1.0, 1.0)
            let region = MKCoordinateRegion(center: location, span: span)
            let mapRect: MKMapRect = helperClass.MKMapRectForCoordinateRegion(region: region)
            let overlay = MapOverlay(identifier: title, coord: location, rect: mapRect)
            mapView.add(overlay)

获取MKMapRect的函数

func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect {
        let topLeft = CLLocationCoordinate2D(latitude: region.center.latitude + (region.span.latitudeDelta/2), longitude: region.center.longitude - (region.span.longitudeDelta/2))
        let bottomRight = CLLocationCoordinate2D(latitude: region.center.latitude - (region.span.latitudeDelta/2), longitude: region.center.longitude + (region.span.longitudeDelta/2))

        let a = MKMapPointForCoordinate(topLeft)
        let b = MKMapPointForCoordinate(bottomRight)

        return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y)))
    }

示例项目可以在 link 中找到

关于ios - MKOverlayRenderer 拉伸(stretch)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41429515/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4