使用真实分辨率将MKMapView渲染为UIImage [英] Rendering MKMapView to UIImage with real resolution
问题描述
我正在使用此函数将MKMapView实例渲染为图像:
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;
}
这很好用。但是对于iphone4,渲染图像的分辨率与设备上的分辨率不同。在设备上,我具有640x920地图视图质量,渲染图像的分辨率为320x460。
然后我将提供给UIGraphicsBeginImageContext()函数的大小加倍,但填充了唯一的左上角图像部分。
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.
问题:有没有办法将地图渲染成具有全分辨率640x920的图像?
Question: Is there any way to get map rendered to image with full resolution 640x920?
推荐答案
尝试使用 UIGraphicsBeginImageContextWithOptions
而不是 UIGraphicsBeginImageContext
:
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
参见 QA1703 了解更多详情。它说:
See QA1703 for more details. It says:
注意:从iOS 4开始,
UIGraphicsBeginImageContextWithOptions
允许你提供一个比例
因子。比例因子为零将
设置为设备
主屏幕的比例因子。这使您可以获得
显示的最清晰,最高分辨率的
快照,包括
Retina Display。
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.
这篇关于使用真实分辨率将MKMapView渲染为UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!