Show MKMapView in UITableViewCell without slow down UI

蹲街弑〆低调 提交于 2019-12-21 15:03:15

问题


i'm trying to put a MKMapView in some UiTableViewCell but (on iPhone 5, so even in other devices), when the app load the cell with the map, the scroll become not too smooth.

There is some method, with GCD or something, to do this in better way?

Here is a screenshot of the result:

I load the Cell from Nib, here is the code where i set the coordinate and the annotation (with custom view, but this is not the problem.)

    // Delegate
    cell.objectMap.delegate = self;

    // Coordinate
    MKCoordinateRegion region;
    region.center = activity.object.location.coordinate;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.055;
    span.longitudeDelta = 0.055;
    region.span = span;
    [cell.objectMap setRegion:region animated:NO];

    // Add an annotation
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = activity.object.location.coordinate;
    [cell.objectMap addAnnotation:point];

    // Show the MKMapView in the cell
    cell.objectMap.hidden = NO;

回答1:


I ended up using MKMapSnapshotter for the devices that support this awesome and fast function and using Google Static Maps Api for the devices that run iOS6. I think is the best and fast solution that i can get.

Thanks to @Rob for the suggestion.

if ( IS_IOS7 ) {

    // Placeholder
    cell.objectImage.image = [UIImage imageNamed:@"mapPlaceholder"];

    // Cooridinate
    MKCoordinateRegion region;
    region.center = activity.object.location.coordinate;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.055;
    span.longitudeDelta = 0.055;
    region.span = span;
    [self.mapViewForScreenshot setRegion:region animated:NO];

    MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    options.region = self.mapViewForScreenshot.region;
    options.scale = [UIScreen mainScreen].scale;
    options.size = CGSizeMake(300, 168);

    MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
    [snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {

        UIImage *image = snapshot.image;
        [cell.objectImage setImage:image];
        cell.mapPinImageView.hidden = NO;

    }];

}else{

    cell.mapPinImageView.hidden = NO;
    [cell.objectImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/staticmap?center=%f,%f&zoom=14&size=600x338&maptype=roadmap&sensor=false&key=APIKEY",activity.object.location.coordinate.latitude, activity.object.location.coordinate.longitude]] placeholderImage:nil];

}




回答2:


Based on your answer, you can also create the snapshot in background and execute the result on the main queue (don't forgive to check the error before update your cell):

if ( IS_IOS7 ) {

    cell.objectImage.image = [UIImage imageNamed:@"mapPlaceholder"];

    // Cooridinate
    MKCoordinateRegion region;
    region.center = activity.object.location.coordinate;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.055;
    span.longitudeDelta = 0.055;
    region.span = span;
    [self.mapViewForScreenshot setRegion:region animated:NO];

    MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    options.region = self.mapViewForScreenshot.region;
    options.scale = [UIScreen mainScreen].scale;
    options.size = CGSizeMake(300, 168);

    MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];

    dispatch_queue_t executeOnBackground = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    [snapshotter startWithQueue:executeOnBackground completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {

        dispatch_async(dispatch_get_main_queue(), ^{
                if (!error) {
                    cell.objectImage.image = snapshot.image;
                    cell.mapPinImageView.hidden = NO;
                } 
            });
        }];
    }];

} else {
    cell.mapPinImageView.hidden = NO;
    [cell.objectImage setImageWithURL:[NSURL URLWithString:@""] placeholderImage:nil];
}


来源:https://stackoverflow.com/questions/20339210/show-mkmapview-in-uitableviewcell-without-slow-down-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!