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

我试图把MKMapView放在一些UiTableViewCell中,但是(在iPhone 5上,所以即使在其他设备上),当应用程序用地图加载单元格时,滚动变得不太平滑。

有一些方法,用GCD或者其他什么方法可以更好地实现这一点?

以下是结果的截图:

在这里输入图像说明

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

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

我最终使用MKMapSnapshotter来支持这个function强大且function强大的设备,并将Google Static Maps Api用于运行iOS6的设备。 我认为是我能得到的最好和最快的解决scheme。

感谢@Rob的build议。

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

在这里输入图像说明

根据你的回答,你也可以在后台创build快照并在主队列上执行结果(在更新你的单元之前不要原谅检查错误):

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