等级
extension MKMapView{
var zoomLevel:Int{
set{
let span = MKCoordinateSpan.init(latitudeDelta: 0, longitudeDelta: 360 / pow(2, Double(newValue)) * Double(self.frame.size.width) / 256)
self.setRegion(MKCoordinateRegion.init(center: self.centerCoordinate, span: span), animated: false)
}
get{
let b = (Double(self.frame.size.width) / 256)
let value = 360 / region.span.longitudeDelta/b
// pow(2, Double(x)) = 360 / region.span.longitudeDelta/b
return Int(sqrt(value))
}
}
//设置缩放级别时调用
func setCenterCoordinate(coordinate: CLLocationCoordinate2D, zoomLevel: Int,
animated: Bool){
let span = MKCoordinateSpan.init(latitudeDelta: 0, longitudeDelta: 360 / pow(2, Double(zoomLevel)) * Double(self.frame.size.width) / 256)
setRegion(MKCoordinateRegion(center: centerCoordinate, span: span), animated: animated)
}
}
地图截图
@IBAction func click1(_ sender: Any) {
self.mapView.zoomLevel = 16
// let option = MKMapSnapshotter.Options.init()
// option.region = self.mapView.region
// option.size = CGSize.init(width: 200, height: 200)
// option.scale = UIScreen.main.scale
// let snapShotter = MKMapSnapshotter.init(options: option)
// snapShotter.start { (snapshot, error) in
// if error == nil{
// self.imgView = UIImageView.init(image: snapshot?.image)
// self.imgView?.frame = CGRect.init(x: 100, y: 100, width: 200, height: 200)
// self.imgView?.isUserInteractionEnabled = true
// let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.tapClick))
// self.imgView?.addGestureRecognizer(tap)
// self.view.addSubview(self.imgView!)
// }
// }
// if let view = self.mapView.snapshotView(afterScreenUpdates: false) {
//
// self.view.addSubview(view)
// }
// self.mapView.centerCoordinate
let point = self.mapView.convert(self.mapView.centerCoordinate, toPointTo: self.mapView)
var rect = CGRect.init(x: 0, y: 0, width: 200, height: 200)
rect.origin.x = point.x - rect.size.width/2
rect.origin.y = point.y - rect.size.height/2
if let view = self.mapView.resizableSnapshotView(from: rect, afterScreenUpdates: true, withCapInsets: UIEdgeInsets.zero) {
// self.view.addSubview(view)
let img = view.snapshot()
self.imgView = UIImageView.init(image: img)
self.imgView?.frame = CGRect.init(x: 100, y: 100, width: 200, height: 200)
// self.imgView?.backgroundColor = .red
self.imgView?.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.tapClick))
self.imgView?.addGestureRecognizer(tap)
self.view.addSubview(self.imgView!)
}
}
extension UIView{
//将当前视图转为UIImage
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
public func snapshot(rect: CGRect = CGRect.zero, scale: CGFloat = UIScreen.main.scale) -> UIImage? {
var snapRect = rect
if __CGSizeEqualToSize(rect.size, CGSize.zero) {
snapRect = calculateSnapshotRect()
}
UIGraphicsBeginImageContextWithOptions(snapRect.size, false, scale)
defer {
UIGraphicsEndImageContext()
}
let siF = self.drawHierarchy(in: snapRect, afterScreenUpdates: false)
if !siF {
self.drawHierarchy(in: snapRect, afterScreenUpdates: true)
}
print("siF == \(siF)")
return UIGraphicsGetImageFromCurrentImageContext()
}
// 计算UIView所显示内容Rect
func calculateSnapshotRect() -> CGRect {
var targetRect = self.bounds
if let scrollView = self as? UIScrollView {
let contentInset = scrollView.contentInset
let contentSize = scrollView.contentSize
targetRect.origin.x = contentInset.left
targetRect.origin.y = contentInset.top
targetRect.size.width = targetRect.size.width - contentInset.left - contentInset.right > contentSize.width ? targetRect.size.width - contentInset.left - contentInset.right : contentSize.width
targetRect.size.height = targetRect.size.height - contentInset.top - contentInset.bottom > contentSize.height ? targetRect.size.height - contentInset.top - contentInset.bottom : contentSize.height
}
return targetRect
}
}