在 3.1 中,我一直在使用“屏幕外”MKMapView 来创建可以旋转、裁剪等的地图图像,然后再将它们呈现给用户。在 3.2 和 4.0 中,这种技术不再适用。这是一些说明问题的代码,然后是我的理论。
// create map view
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, MAP_FRAME_SIZE, MAP_FRAME_SIZE)];
_mapView.zoomEnabled = NO;
_mapView.scrollEnabled = NO;
_mapView.delegate = self;
_mapView.mapType = MKMapTypeSatellite;
// zoom in to something enough to fill the screen
MKCoordinateRegion region;
CLLocationCoordinate2D center = {30.267222, -97.763889};
region.center = center;
MKCoordinateSpan span = {0.1, 0.1 };
region.span = span;
_mapView.region = region;
// set scrollview content size to full the imageView
_scrollView.contentSize = _imageView.frame.size;
// force it to load
#ifndef __IPHONE_3_2
// in 3.1 we can render to an offscreen context to force a load
UIGraphicsBeginImageContext(_mapView.frame.size);
[_mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
#else
// in 3.2 and above, the renderInContext trick doesn't work...
// this at least causes the map to render, but it's clipped to what appears to be
// the viewPort size, plus some padding
[self.view addSubview:_mapView];
#endif
地图加载完成后,我会拍下它的照片并将其填充到我的滚动视图中
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
NSLog(@"[MapBuilder] mapViewDidFinishLoadingMap");
// render the map to a UIImage
UIGraphicsBeginImageContext(mapView.bounds.size);
// the first sub layer is just the map, the second is the google layer, this sublayer structure might change of course
[[[mapView.layer sublayers] objectAtIndex:0] renderInContext:UIGraphicsGetCurrentContext()];
// we are done with the mapView at this point, we need its ram!
_mapView.delegate = nil;
[_mapView release];
[_mapView removeFromSuperview];
_mapView = nil;
UIImage* mapImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
_imageView.image = mapImage;
[mapImage release], mapImage = nil;
}
第一个问题是在 3.1 中渲染到上下文会触发地图开始加载。这不再适用于 3.2、4.0。我发现唯一会触发加载的是将地图临时添加到视图中(即使其可见)。问题是地图只渲染到屏幕的可见区域,加上一点填充。框架/边界很好,但它似乎“有帮助”优化了加载以将图块限制为屏幕上可见或靠近它的图块。
任何想法如何强制地图以全尺寸加载?其他人有这个问题吗?