【发布时间】:2014-06-02 20:20:03
【问题描述】:
我有一个应用程序,它在 MapView 中显示大约 100 个注释(带有自定义图钉图像、标注附件和注释图像)。在构建注释时,我会在注释和构建之间存储一个链接,这样我就可以分配正确的建筑物并在之后打开正确的 segue。
在 iOS 6 中,它们的构建速度非常快,我还在添加它们时启用了动画,所以一个图钉一个接一个地掉了,但是在 iOS7 中使用苹果地图,这不再可能了(?)。现在,在我的 iPhone 4S 上构建这 100 条注释需要 1 秒以上,这太长了。有没有办法改进代码?
- (void)viewDidLoad
...
//creating annotations
annotationlink = [[NSMutableArray alloc] init];
for (int i = 0; i < data.count; i++) {
NSDictionary *dataItem = [data objectAtIndex:i];
//storing annotation in array for link
Annotation *buildingannotation = [[Annotation alloc] init];
NSNumber *index = [NSNumber numberWithInteger:i];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:index, indexKey, buildingannotation, annotationKey, nil];
[annotationlink addObject:dict];
buildingannotation.title = [dataItem objectForKey:@"Building"];
buildingannotation.subtitle = [dataItem objectForKey:@"Info"];
MKCoordinateRegion buildingcoordinates;
buildingcoordinates.center.latitude = [[dataItem objectForKey:@"Latitude"] floatValue];
buildingcoordinates.center.longitude = [[dataItem objectForKey:@"Longitude"] floatValue];
buildingannotation.coordinate = buildingcoordinates.center;
[self.mapView addAnnotation:buildingannotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
MKAnnotationView *pinView = (MKAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];
MKAnnotationView *customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
customAnnotationView.canShowCallout = YES;
//right button to detail view
UIButton* disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customAnnotationView.rightCalloutAccessoryView = disclosureButton;
//left button for image
NSInteger *buildingindex = [self getIndex:annotation];
NSDictionary *dataItem = [data objectAtIndex:buildingindex];
NSString* filename = [dataItem objectForKey:@"Thumb"];
filename = [filename stringByAppendingString:@"@2x.jpg"];
NSString* resourceimagePath = [resourcePath stringByAppendingPathComponent:filename];
Image = [UIImage imageWithContentsOfFile:resourceimagePath];
UIImageView *AnnotationThumb = [[UIImageView alloc] initWithImage:Image];
AnnotationThumb.frame = CGRectMake(0, 0, 31, 31);
customAnnotationView.leftCalloutAccessoryView = AnnotationThumb;
//annotation image
customAnnotationView.image = [UIImage imageNamed:@"Annotation_white.png"];
return customAnnotationView;
return pinView;
}
以下函数使用 nspredicate 获取当前注释的索引,以使用字典过滤数组。这样做的好处是,我也可以在 calloutAccessoryControlTapped 时使用它:
-(NSInteger*) getIndex:(Annotation*)searchannotation
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", annotationKey, searchannotation];
NSArray *filteredarray = [annotationlink filteredArrayUsingPredicate:predicate];
NSDictionary *building = [filteredarray objectAtIndex:0];
NSInteger *buildingIndex = [[building objectForKey:indexKey] integerValue];
return buildingIndex;
}
对于 iPhone 4S,最后一个 pin 是在视图加载后 1.14 秒构建的。
如果我手动搜索注释链接数组,而不是像这样使用 nspredicate 函数:
//left button for image
int buildingIndex;
for (int i = 0; i < annotationlink.count; i++) {
NSDictionary *annotationDict = [annotationlink objectAtIndex:i];
if ([[annotationDict objectForKey:annotationKey] isEqual:annotation]) {
buildingIndex= [[annotationDict objectForKey:indexKey] integerValue];
i = annotationlink.count;
}
}
NSDictionary *dataItem = [data objectAtIndex:buildingIndex];
日志说最后一个 pin 是在 viewDidLoad 之后 1.89 秒构建的。
如果我在 viewDidApper 而不是 viewDidLoad 中创建注释,则视图会立即显示偏离路线,但背景需要一些时间才能加载,因此在删除引脚之前,所有内容都是灰色的,这也不是很好...
【问题讨论】:
-
好吧,我不得不提到延迟来自
(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation-方法中的代码。当我记录它时,每个引脚之间有时会有高达 0.02 秒的间隙...... -
每次地图需要显示该注解时都会调用 viewForAnnotation 委托方法(对于同一个注解可以多次调用)。
annotationLink数组没有必要。相反,将数据或对每个注释所需的数据的引用放在 Annotation 类本身中,并在添加注释之前设置此数据。有关想法,请参阅 stackoverflow.com/questions/7921106/…。 -
对于使用所选注释的数据进行转场(不使用这种“annotationLink”方法并使用注释本身作为数据源),请参阅此答案以获取示例:stackoverflow.com/questions/14805954/…
-
代码中还有其他一些低效率的地方(viewForAnnotation 忽略了出队结果,在 viewForAnnotation 中计算图像文件名等),但我目前无法详细回答。
-
我之前也遇到过这个问题,发现在主线程上运行时,运行速度更快。
标签: ios ios7 annotations mkmapview mkannotationview