Stop MKMapView from reloading

2019-08-02 11:36发布

问题:

I have a MKMapView with scrolling and userInteraction disabled within a UITableViewCell. The desired effect (effectively a static image of the map in a certain position) works really well but when the MKMapView drifts on and off screen (scrolling) it reloads the map, which occasionally causes the app to crash. I have loaded the custom UITableViewCell in like any other UITableViewCell in cellForRowAtIndexPath:

if(indexPath.section == 0 && indexPath.row == 0)
{
    MapTableViewCell *cell = (MapTableViewCell *)[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@Map", cellIdentifier]];

    if(cell == nil)
    {
        cell = [[[MapTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
    }

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MapTableViewCell" owner:self options:nil];

    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[UITableViewCell class]])
        {
            cell = (MapTableViewCell *)currentObject;
            break;
        }
    }

    return cell;
}

I've found out that with this current method, if you let the map image load before moving the UITableView then it's OK. But if you move it off the screen before it's finished loading then it will crash! :(

I will just point out that I do not want to be able to control the map in any way or show any annotations on it. I did try to screenshot a map view, hide it from screen and display that screenshot as a UIImageView in the UITableViewCell but this wasn't fast enough!

EDIT: Updated code. This is the full code for this method. Is my custom TableViewCell alloc incorrect here?

回答1:

Try setting the region of the map/the scope of the map to what you are desiring and then change the properties of userInteractionEnabled and zoomEnabled. So probably something like this:

MKCoordinateRegion region;
region.center = "location";  //a CLLocationCoordinate2D location of where ever

MKCoordinateSpan span;
span.latitudeDelta = 0.03; //desired size for both latDelta and lonDelta
span.longitudeDelta = 0.03;
region.span = span;

[mapView setRegion:region animated:YES];

And then for the properties try this:

mapView.zoomEnabled = NO;
mapView.userInteractionEnabled = NO;


回答2:

Thanks to tc for the answer (the comment above).

What was required was for me to release the MKMapView in the custom UITableViewCell dealloc method.