以真实分辨率将 MKMapView 渲染为 UIImage

发布于 2024-10-03 05:27:39 字数 557 浏览 1 评论 0原文

我正在使用此函数将 MKMapView 实例渲染为图像:

@implementation UIView (Ext)
- (UIImage*) renderToImage
{
  UIGraphicsBeginImageContext(self.frame.size);
  [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext(); 
  return image;
}

这工作正常。但对于 iphone4,渲染图像的分辨率与设备上实际的分辨率不同。在设备上,我的地图视图质量为 640x920,渲染图像的分辨率为 320x460。 然后,我将提供给 UIGraphicsBeginImageContext() 函数的大小加倍,但填充了唯一的左上角图像部分。

问题:有什么方法可以将地图渲染为全分辨率 640x920 的图像吗?

I am using this function for rendering MKMapView instance into image:

@implementation UIView (Ext)
- (UIImage*) renderToImage
{
  UIGraphicsBeginImageContext(self.frame.size);
  [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext(); 
  return image;
}

This works fine. But with iphone4 the rendered image doesn't have same resolution as it has really on device. On device I have the 640x920 map view quality, and rendered image has the resolution 320x460.
Then I doubled the size that is provided to UIGraphicsBeginImageContext() function but that filled the only top-left image part.

Question: Is there any way to get map rendered to image with full resolution 640x920?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-10-10 05:27:40

iOS 7 引入了一种新方法来生成 MKMapView 的屏幕截图。现在可以使用新的 MKMapSnapshot API,如下所示:

MKMapView *mapView = [..your mapview..]

MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc]init];
options.region = mapView.region;
options.mapType = MKMapTypeStandard;
options.showsBuildings = NO;
options.showsPointsOfInterest = NO;
options.size = CGSizeMake(1000, 500);

MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc]initWithOptions:options];
[snapshotter startWithQueue:dispatch_get_main_queue() completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
    if( error ) {
        NSLog( @"An error occurred: %@", error );
    } else {
       [UIImagePNGRepresentation( snapshot.image ) writeToFile:@"/Users/<yourAccountName>/map.png" atomically:YES];
    }
}];

目前,所有叠加层和注释均未渲染。之后您必须自己将它们渲染到生成的快照图像上。提供的 MKMapSnapshot 对象有一个方便的辅助方法来完成坐标和点之间的映射:

CGPoint point = [snapshot pointForCoordinate:locationCoordinate2D];

iOS 7 introduced a new method to generate screenshots of a MKMapView. It is now possible to use the new MKMapSnapshot API as follows:

MKMapView *mapView = [..your mapview..]

MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc]init];
options.region = mapView.region;
options.mapType = MKMapTypeStandard;
options.showsBuildings = NO;
options.showsPointsOfInterest = NO;
options.size = CGSizeMake(1000, 500);

MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc]initWithOptions:options];
[snapshotter startWithQueue:dispatch_get_main_queue() completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
    if( error ) {
        NSLog( @"An error occurred: %@", error );
    } else {
       [UIImagePNGRepresentation( snapshot.image ) writeToFile:@"/Users/<yourAccountName>/map.png" atomically:YES];
    }
}];

Currently all overlays and annotations are not rendered. You have to render them afterwards onto the resulting snapshot image yourself. The provided MKMapSnapshot object has a handy helper method to do the mapping between coordinates and points:

CGPoint point = [snapshot pointForCoordinate:locationCoordinate2D];
心是晴朗的。 2024-10-10 05:27:39

尝试使用 UIGraphicsBeginImageContextWithOptions 而不是 UIGraphicsBeginImageContext

UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);

请参阅 QA1703 了解更多详情。它说:

注意:从 iOS 4 开始,
UIGraphicsBeginImageContextWithOptions
允许您提供秤
因素。将比例因子设置为零
到设备的比例因子
主屏幕。这使您能够获得
最锐利、最高分辨率
显示快照,包括
视网膜显示屏。

Try using UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext:

UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);

See QA1703 for more details. It says:

Note: Starting from iOS 4,
UIGraphicsBeginImageContextWithOptions
allows you to provide with a scale
factor. A scale factor of zero sets it
to the scale factor of the device's
main screen. This enables you to get
the sharpest, highest-resolustion
snapshot of the display, including a
Retina Display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文