Off-screen MKMapView behaves differently in 3.2, 4.0

In 3.1, I used the “off-screen” MKMapView to create map images that I can rotate, crop, etc., before presenting them to the user. In 3.2 and 4.0, this method no longer works completely correctly. Here is some code that illustrates the problem, and then my theory.

     // 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 clipped to what appears to be    
     // the viewPort size, plus some padding    
     [self.view addSubview:_mapView];          

#endif

when the map is loaded, I click it and write it to my scrollview

- (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. , , , (.. ). , , . / , , , "" , , .

, ? - ?

+3

Source: https://habr.com/ru/post/1747586/


All Articles