Ios 在UITableViewCell中显示MKMapView而不减慢UI
我试图在一些UiTableViewCell中放置一个MKMapView,但是(在iPhone 5上,甚至在其他设备上),当应用程序加载带有地图的单元格时,滚动变得不太平滑 有什么方法,用GCD或者别的什么,可以更好地做到这一点吗 以下是结果的屏幕截图: 我从Nib加载单元格,下面是设置坐标和注释的代码(使用自定义视图,但这不是问题所在)Ios 在UITableViewCell中显示MKMapView而不减慢UI,ios,iphone,objective-c,uitableview,mkmapview,Ios,Iphone,Objective C,Uitableview,Mkmapview,我试图在一些UiTableViewCell中放置一个MKMapView,但是(在iPhone 5上,甚至在其他设备上),当应用程序加载带有地图的单元格时,滚动变得不太平滑 有什么方法,用GCD或者别的什么,可以更好地做到这一点吗 以下是结果的屏幕截图: 我从Nib加载单元格,下面是设置坐标和注释的代码(使用自定义视图,但这不是问题所在) 我最终使用MKMapSnapshotter来支持这个强大而快速的功能,并对运行iOS6的设备使用googlestaticmapsapi。我认为这是我能得到的最
我最终使用MKMapSnapshotter来支持这个强大而快速的功能,并对运行iOS6的设备使用googlestaticmapsapi。我认为这是我能得到的最好、最快的解决方案 谢谢你的建议
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];
}
根据您的回答,您还可以在后台创建快照,并在主队列上执行结果(请不要忘记在更新单元格之前检查错误):
它需要实际的地图视图吗?出于性能原因,您通常希望拍摄地图快照并插入该图像,而不是实际的地图视图。iOS 7有一个专门为此设计的
MKMapSnapshotter
。或者你也需要支持早期版本吗?是的,我也需要支持iOS6。MKMapSnapshotter很酷,速度也很快。iOS6中也有类似的东西?当然没有什么比iOS7的MKMapSnapshotter
更优雅的了。我见过一些手动尝试使用mapViewDidFinishLoadingMap
和renderInContext
,但它非常难看。
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];
}
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];
}