【问题标题】:Building MapView Annotations is very slow构建 MapView 注释非常慢
【发布时间】: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 &lt;MKAnnotation&gt;)annotation-方法中的代码。当我记录它时,每个引脚之间有时会有高达 0.02 秒的间隙......
  • 每次地图需要显示该注解时都会调用 viewForAnnotation 委托方法(对于同一个注解可以多次调用)。 annotationLink 数组没有必要。相反,将数据或对每个注释所需的数据的引用放在 Annotation 类本身中,并在添加注释之前设置此数据。有关想法,请参阅 stackoverflow.com/questions/7921106/…。
  • 对于使用所选注释的数据进行转场(不使用这种“annotationLink”方法并使用注释本身作为数据源),请参阅此答案以获取示例:stackoverflow.com/questions/14805954/…
  • 代码中还有其他一些低效率的地方(viewForAnnotation 忽略了出队结果,在 viewForAnnotation 中计算图像文件名等),但我目前无法详细回答。
  • 我之前也遇到过这个问题,发现在主线程上运行时,运行速度更快。

标签: ios ios7 annotations mkmapview mkannotationview


【解决方案1】:

感谢安娜的建议!我实现了这样的改进:

Annotation.h:

#import <MapKit/MKAnnotation.h> 

@interface Annotation : NSObject <MKAnnotation> {} 
@property(nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property(nonatomic, copy) NSString *title; 
@property(nonatomic, copy) NSString *subtitle; 
@property NSInteger *ID; 
@end 

Annotation.m:

#import "Annotation.h" 

@implementation Annotation 
@synthesize coordinate, title, subtitle, ID; 
@end 

ViewDidAppear:

//creating annotations 
for (int i = 0; i < data.count; i++) { 
   NSDictionary *dataItem = [data objectAtIndex:i]; 
   Annotation *buildingannotation = [[Annotation alloc] init]; 
   buildingannotation.ID = i; 
   buildingannotation.title = [dataItem objectForKey:@"Building"]; 
   buildingannotation.subtitle = [dataItem objectForKey:@"Subtitle"]; 

   MKCoordinateRegion buildingcoordinates; 
   buildingcoordinates.center.latitude = [[dataItem objectForKey:@"Latitude"] floatValue];    
   buildingcoordinates.center.longitude = [[dataItem objectForKey:@"Longitude"] floatValue]; 
   buildingannotation.coordinate = buildingcoordinates.center; 

   [self.mapView addAnnotation:buildingannotation]; 
} 

viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]]){
        return nil;
}

MKAnnotationView *pinView = (MKAnnotationView *)
[self.mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];

if (pinView == nil) {
    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
    Annotation *buildingAnnotation = (Annotation *)annotation;
    NSInteger *buildingindex = buildingAnnotation.ID;

    NSString *filePath = [thumbname objectAtIndex:buildingindex];
    UIImageView *AnnotationThumb = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
    AnnotationThumb.frame = CGRectMake(0, 0, 31, 31);
    customAnnotationView.leftCalloutAccessoryView = AnnotationThumb;

    //annotation image
    customAnnotationView.image = [UIImage imageNamed:@"Annotation_white.png"];

    return customAnnotationView;
} else {
    pinView.annotation = annotation;
}

return pinView;
}

标注:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    Annotation *buildingAnnotation = (Annotation *)view.annotation;
    selectedbuilding = buildingAnnotation.ID;
    [self performSegueWithIdentifier:@"DetailViewController" sender:self];
}

显示所有注释还需要一些时间。有没有机会进一步改进代码?

【讨论】:

  • 我发现上面的代码有问题。第一次使用正确的图像完全正确地构建引脚。当它们被重用时,它们会显示正确的标题,当我点击它们时,会打开正确的 segue。但是 AnnotationThumb 别名 leftCalloutAccessoryView 在重复使用的引脚上显示了不同的图像。怎么了?
  • 由于 leftCalloutAccessoryView 是特定于注解的,因此您必须确定并设置它的图像是否正在重用视图(这意味着在 if-else 之后就在 return pinView; 之前)。现在,它仅在为某些注释 x 创建视图时设置。如果某个注释 y 重用了该视图,它仍然具有注释 x 的图像。
【解决方案2】:

我更新了关于 Anna 的回复和 PhotosByLocation Sample Application 的 vievForAnnotation 函数。它现在可以工作了,我希望这是实现重用的正确方法......

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]]){
        return nil;
    }

    MKAnnotationView *buildingAnnotationView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];

    if (buildingAnnotationView) {
        [buildingAnnotationView prepareForReuse];
    } else {
        buildingAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
        buildingAnnotationView.canShowCallout = YES;

        //right button to detail view
        UIButton* disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        buildingAnnotationView.rightCalloutAccessoryView = disclosureButton;

        //annotation image
        buildingAnnotationView.image = [UIImage imageNamed:@"Annotation_white.png"];
    }

    //left button for image
    Annotation *buildingAnnotation = (Annotation *)annotation;
    NSInteger *buildingindex = buildingAnnotation.ID;

    NSString *filePath = [thumbname objectAtIndex:buildingindex];
    UIImageView *AnnotationThumb = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
    AnnotationThumb.frame = CGRectMake(0, 0, 31, 31);
    buildingAnnotationView.leftCalloutAccessoryView = AnnotationThumb;

    return buildingAnnotationView;
}

【讨论】:

  • 我认为您误用了 prepareForReuse。 Apple 文档 prepareForReuse 是非功能性的,并且打算在视图返回到队列时调用,而不是在获取它以供重用时调用。我想如果它只有弱引用,可以用 nil prepareForReuse 释放它,但我认为你需要重写 prepareForReuse 以在它返回到空闲队列之前释放特定于注释的位。
猜你喜欢
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多