【问题标题】:Render part of MKMapView to image将 MKMapView 的一部分渲染为图像
【发布时间】:2013-02-04 09:36:32
【问题描述】:

我有兴趣捕捉地图的一部分以创建您在弹出框顶部看到的图像。我想我会捕获一个 UIImage,你可以从图钉的坐标开始吗?

谢谢

【问题讨论】:

    标签: ios mkmapview uipopovercontroller mapkit


    【解决方案1】:

    你可以试试这样的:

    - (UIImage*) imageFromView:(UIView*)view rect:(CGRect)rect {
        // capture the full view in an image
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage* viewImg = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        // crop down to just our desired rectangle, accounting for Retina scale 
        CGFloat scale = [[UIScreen mainScreen] scale];
        CGRect scaledRect = CGRectMake(scale * rect.origin.x, scale * rect.origin.y,
                                       scale * rect.size.width, scale * rect.size.height);
        CGImageRef resultImgRef = CGImageCreateWithImageInRect([viewImg CGImage], scaledRect);
        UIImage* result = [UIImage imageWithCGImage: resultImgRef
                                              scale: 1.0f / scale
                                        orientation: UIImageOrientationUp];
    
        CGImageRelease(resultImgRef);
        return result;
    }
    
    - (IBAction)onButtonTapped:(id)sender {
        // just pick the map's center as the location to capture.  could be anything.
        CLLocationCoordinate2D center = self.map.centerCoordinate;
        // convert geo coordinates to screen (view) coordinates
        CGPoint point = [self.map convertCoordinate:center toPointToView: self.map];
    
        // make this span whatever you want - I use 120x80 points
        float width = 120.0;
        float height = 80.0;
        // here's the frame in which we'll capture the underlying map
        CGRect frame = CGRectMake(point.x - width / 2.0,
                                  point.y - height / 2.0,
                                  width, height);
    
        // just show the captured image in a UIImageView overlay 
        //  in the top, left corner, with 1:1 scale
        UIImageView* overlay = [[UIImageView alloc] initWithImage: [self imageFromView: self.map rect: frame]];
        frame.origin = CGPointZero;
        overlay.frame = frame;
        [self.view addSubview: overlay];
    }
    

    imageFromView:rect: 方法仅在给定视图内捕获给定矩形区域,并生成UIImage。

    onButtonTapped 方法使用第一种方法捕获我的地图中心周围的区域,并将捕获的图像显示在屏幕的左上角。当然,您可以将地图 center 替换为 pin 的坐标,使区域宽度/高度随心所欲,然后将生成的图像放入弹出视图中。

    这只是一个演示。在将地图平移到我想要的位置后,我在示例应用程序中使用了一个按钮来触发捕获。

    结果

    限制

    我的代码只是以 1:1 的大小比例显示捕获的矩形。当然,如果您愿意,您可以将捕获的UIImage 放入UIImageView 中,您可以随意缩放它。您可以将图像放大。但是,如果这样做,您将失去图像清晰度。这个过程只是对UIView 进行屏幕截图。它不能直接处理地图数据,所以当你放大它(以更大的尺寸显示图像)时,图像不会像你实际放大 MKMapView 时那样锐化。

    【讨论】:

    • 只是想指出,在视网膜显示器上,叠加层显示的是双倍尺寸的图像。
    • 我的解决方案是使用 stackoverflow.com/questions/3504173/detect-retina-display 检查视网膜显示器,然后在裁剪前将 frame.size.width 和 height 乘以 2。
    • @Jenni.b,非常感谢!我更新了我的答案以修复您发现的错误。
    【解决方案2】:

    在iOS7 上,您可以使用MKMapSnapshotter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 2019-05-16
      • 1970-01-01
      相关资源
      最近更新 更多