我正在尝试将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;
答案 0 :(得分: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 :(得分:3)
根据您的回答,您还可以在后台创建快照并在主队列上执行结果(在更新您的单元格之前不要原谅检查错误):
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];
}