首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKAnnotationView图像转换问题

MKAnnotationView图像转换问题
EN

Stack Overflow用户
提问于 2015-05-08 11:23:15
回答 1查看 896关注 0票数 1

我有一个装载了大量自定义注释的MKMapView (800个左右)。

当我拖动地图并返回到一个注释时,它的图像与另一个注释发生了变化。对我来说,这似乎是一个缓存问题。

拖曳前销

拖曳后销

SuperClass报头

代码语言:javascript
复制
#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

SubClass (创建问题的那个)标头

代码语言:javascript
复制
#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委托方法

代码语言:javascript
复制
- (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法

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-09 15:27:38

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

代码语言:javascript
复制
if (annotationView == nil) {
    annotationView = [location getAnnotationView];
} else {
    annotationView.annotation = annotation;
}

但是注释视图的image没有更新--在脱队列视图中,image将被设置为与视图最初创建的注释相关联的注释(当getAnnotationView被调用时)。

因此,现在视图出现在新注释的坐标处,但图像仍然来自视图所使用的前一个注释。

修复这个问题有多种方法,例如创建一个适当的MKAnnotationView子类,它监视对其annotation属性的更改,并自动更新与注释相关的所有其他属性。

使用现有代码,修复它的一个简单方法是将特定于注释的属性分离为一个单独的方法,在创建视图和更新其annotation属性时都可以调用该方法。

例如,在CompanyAnnotation中,创建如下方法:

代码语言:javascript
复制
-(void)configureView:(MKAnnotationView *)av
{
    av.image = self.pinImage;
}

然后将getAnnotationView更改为:

代码语言:javascript
复制
- (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

代码语言:javascript
复制
if (annotationView == nil) {
    annotationView = [location getAnnotationView];
} else {
    annotationView.annotation = annotation;

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

请注意,MapAnnotation将在pinImageName属性中遇到同样的问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30122625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档