MKOverlayRenderer拉伸图像 [英] MKOverlayRenderer stretches image

查看:85
本文介绍了MKOverlayRenderer拉伸图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像放置在地图叠加层中,该叠加层会与地图一起缩放.使用以下代码,图像会出现在地图视图中,但会进行拉伸以适合视图.如何将图像的原始长宽比保持在叠加层内?

I would like to place an image in a map overlay which will scale along with the map. Using the code below, the image appears in the map view but it is stretched to fit the view. How can I keep the original aspect ratio of the image inside the overlay?

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()
        }
    }
}

推荐答案

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

In your ViewController.swift, in viewDidLoad(),

let rec = mapview.visibleMapRect

对此进行更改, rec 应该与所使用图像的大小完全相同.

Change this, rec should be exactly same size of the image used.

            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的函数

The function to get 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)))
    }

可以在以下链接

这篇关于MKOverlayRenderer拉伸图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆