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 では機能しなくなりました。私が見つけた唯一のことは、マップを一時的にビューに追加することです(つまり、表示するようにします)。問題は、マップが画面の表示領域にのみレンダリングされ、さらに少しのパディングが加えられることです。フレーム/境界は問題ありませんが、読み込みを最適化して、タイルを画面上またはその近くに表示されるものに制限しているように見えます.
マップを強制的にフルサイズでロードする方法はありますか? 他の誰かがこの問題を抱えていますか?