Iphone 以真实分辨率将MKMapView渲染为UIImage
我正在使用此函数将MKMapView实例渲染为图像:Iphone 以真实分辨率将MKMapView渲染为UIImage,iphone,uiimage,mapkit,Iphone,Uiimage,Mapkit,我正在使用此函数将MKMapView实例渲染为图像: @implementation UIView (Ext) - (UIImage*) renderToImage { UIGraphicsBeginImageContext(self.frame.size); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageCo
@implementation UIView (Ext)
- (UIImage*) renderToImage
{
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
这个很好用。但在iphone4上,渲染图像的分辨率与设备上的分辨率不同。在设备上,我具有640x920地图视图质量,渲染图像的分辨率为320x460。
然后,我将提供给UIGraphicsBeginImageContext()函数的大小增加了一倍,但只填充了左上方的图像部分
问题:有没有办法将地图渲染成全分辨率640x920的图像?尝试使用
UIGraphicsBeginImageContextWithOptions
而不是UIGraphicsBeginImageContext
:
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
有关更多详细信息,请参阅。它说:
注意:从iOS 4开始,
UIGraphicsBeginImageContextWithOptions
允许您提供电子秤
因素比例因子为零将设置它
到设备的比例因子
主屏幕。这使您能够
最尖锐、最高的分辨力
显示的快照,包括
视网膜显示
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];
}
}];
我在这一行中遇到错误
options.mapType=MKMapTypeStandard代码>我如何解决它。@KhushbuDesai你在使用Swift吗?是的,我得到了解决方案options.mapType=.standard
CGPoint point = [snapshot pointForCoordinate:locationCoordinate2D];