在UITableViewCell中显示MKMapView而不会减慢UI

 我就是在刷粪_944 发布于 2023-02-13 12:46

我试图将MKMapView放在一些UiTableViewCell中,但是(在iPhone 5上,甚至在其他设备中),当应用程序使用地图加载单元格时,滚动变得不太顺畅.

有一些方法,使用GCD或其他方法,以更好的方式做到这一点?

以下是结果的屏幕截图:

在此输入图像描述

我从Nib加载Cell,这里是我设置坐标和注释的代码(使用自定义视图,但这不是问题.)

    // 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;

Monte.. 15

我最终将MKMapSnapshotter用于支持这种超棒和快速功能的设备,并使用Google Static Maps Api来运行运行iOS6的设备.我认为这是我能得到的最好,最快的解决方案.

感谢@Rob的建议.

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];

}

在此输入图像描述

1 个回答
  • 我最终将MKMapSnapshotter用于支持这种超棒和快速功能的设备,并使用Google Static Maps Api来运行运行iOS6的设备.我认为这是我能得到的最好,最快的解决方案.

    感谢@Rob的建议.

    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];
    
    }
    

    在此输入图像描述

    2023-02-13 12:49 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有