使用真实分辨率将MKMapView渲染为UIImage

Prc*_*ela 8 iphone uiimage mapkit

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

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

这很好用.但是使用iphone4时,渲染图像的分辨率与设备上的分辨率不同.在设备上我具有640x920地图视图质量,渲染图像具有320x460的分辨率.然后我将提供给UIGraphicsBeginImageContext()函数的大小加倍,但填充了唯一的左上角图像部分.

问题:有没有办法将地图渲染为具有全分辨率640x920的图像?

小智 8

尝试使用UIGraphicsBeginImageContextWithOptions而不是UIGraphicsBeginImageContext:

UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅QA1703.它说:

注意:从iOS 4开始,UIGraphicsBeginImageContextWithOptions允许您提供比例因子.比例因子为零将其设置为设备主屏幕的比例因子.这使您可以获得显示器的最清晰,最高分辨率的快照,包括Retina显示屏.