【问题标题】:MKAnnotationView image changing issueMKAnnotationView 图像更改问题
【发布时间】:2015-05-08 11:23:15
【问题描述】:

我有一个加载了大量自定义注释的 MKMapView(大约 800 个)。

当我在地图上拖动并返回注解时,它的图像已更改为另一个。对我来说,这似乎是一个缓存问题。

拖动前固定

拖动后固定

超类标头

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapAnnotation : NSObject <MKAnnotation>

@property (nonatomic, copy) NSString *title;
@property NSString * pinImageName;
@property (nonatomic) CLLocationCoordinate2D coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title pinImageName:(NSString *)pinImageName;

- (MKAnnotationView *)getAnnotationView;
@end

子类(产生问题的那个)标题

#import "MapAnnotation.h"
#import "Company.h"

@interface CompanyAnnotation : MapAnnotation

@property Company *company;
@property UIImage *pinImage;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title pinImage:(UIImage *)pinImage;
- (MKAnnotationView *)getAnnotationView;

@end

viewForAnnotation 委托方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    if (annotation == mapView.userLocation){
        return nil;
    }

    MapAnnotation *location = [MapAnnotation new];
    MKAnnotationView *annotationView = [MKAnnotationView new];

    if ([annotation isKindOfClass:[CompanyAnnotation class]]) {
        location = (CompanyAnnotation *)annotation;
        annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"companyAnnotation"];
    }

    if (annotationView == nil) {
        annotationView = [location getAnnotationView];
    } else {
        annotationView.annotation = annotation;
    }
    return annotationView;
}

getAnnotationView 方法

- (MKAnnotationView *)getAnnotationView {
    MKAnnotationView * annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"companyAnnotation"];
    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];
    [annotationView setContentMode:UIViewContentModeScaleAspectFit];
    [annotationView setImage:self.pinImage];
    [annotationView setFrame:CGRectMake(0, 0, 28, 44)];
    [annotationView setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];

    return annotationView;
}

【问题讨论】:

  • 您能否显示viewForAnnotation 委托方法和getAnnotationView 方法的代码?听起来出队没有得到正确处理(正如你所说的“缓存”问题)。
  • 您的问题没有提到实现的主要部分,即注释创建。请描述性。

标签: ios mkmapview mkannotation mkannotationview


【解决方案1】:

viewForAnnotation 中没有正确处理出队,因为当dequeueReusableAnnotationViewWithIdentifier 返回以前使用的视图时(当annotationView 不是nil 时),代码仅更新该视图的annotation 属性:

if (annotationView == nil) {
    annotationView = [location getAnnotationView];
} else {
    annotationView.annotation = annotation;
}

但是注释视图的image 没有更新——在出队视图中,image 将设置为与视图最初创建的注释相关联(当@ 987654329@ 被调用)。

因此,现在视图出现在新注释的坐标处,但图像仍来自该视图用于的先前注释。


有多种方法可以解决此问题,例如创建一个适当的 MKAnnotationView 子类,以监控对其 annotation 属性的更改并自动更新与注释关联的所有其他属性。

使用现有代码,解决此问题的一种简单方法是将特定于注解的属性更改分离到一个单独的方法中,该方法可以在创建视图时和更新其 annotation 属性时调用。

例如,在CompanyAnnotation 中,创建这样的方法:

-(void)configureView:(MKAnnotationView *)av
{
    av.image = self.pinImage;
}

然后将getAnnotationView改为:

- (MKAnnotationView *)getAnnotationView {
    MKAnnotationView * annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"companyAnnotation"];

    //set properties here that are not specific to an annotation...
    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];
    [annotationView setContentMode:UIViewContentModeScaleAspectFit];
    //[annotationView setImage:self.pinImage];
    [annotationView setFrame:CGRectMake(0, 0, 28, 44)];
    [annotationView setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];

    //set properties that are specific to an annotation...
    [self configureView:annotationView];

    return annotationView;
}

终于在viewForAnnotation:

if (annotationView == nil) {
    annotationView = [location getAnnotationView];
} else {
    annotationView.annotation = annotation;

    if ([annotation isKindOfClass:[CompanyAnnotation class]]) {
        //update image, etc...
        [annotation configureView:annotationView];
    }
}

请注意,MapAnnotationpinImageName 属性有相同的问题。

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多