Ios 为什么我的自定义注释图像在更改?

Ios 为什么我的自定义注释图像在更改?,ios,objective-c,cocoa-touch,mapkit,mkannotationview,Ios,Objective C,Cocoa Touch,Mapkit,Mkannotationview,我有21个不同的图像,用于在地图上进行自定义注释。当我最初加载地图时,一切都很完美。当我离开这个区域并返回时,图像似乎发生了变化。注释引脚不再与正确的图像对应(但是当我按下注释时,标题和副标题仍然正确)。这就像是图像突然变得混乱。顺便说一下,我只是将测试信息输入到NSUserDefaults中,并使用它来测试在地图上显示自定义注释的功能。我假设这与注释视图的可重用性有关,但我无法正确地编写代码 MapViewController.h #import "ViewController.h" #imp

我有21个不同的图像,用于在地图上进行自定义注释。当我最初加载地图时,一切都很完美。当我离开这个区域并返回时,图像似乎发生了变化。注释引脚不再与正确的图像对应(但是当我按下注释时,标题和副标题仍然正确)。这就像是图像突然变得混乱。顺便说一下,我只是将测试信息输入到
NSUserDefaults
中,并使用它来测试在地图上显示自定义注释的功能。我假设这与注释视图的可重用性有关,但我无法正确地编写代码

MapViewController.h

#import "ViewController.h"
#import <MapKit/MapKit.h>

@interface MapViewController : ViewController<MKMapViewDelegate>
{
    NSUserDefaults *defaults;
    NSMutableArray *reportedSightings;
}

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@end
#导入“ViewController.h”
#进口
@接口MapViewController:ViewController
{
NSUserDefaults*默认值;
NSMutableArray*报告的目击事件;
}
@属性(强,非原子)IBMMapView*mapView;
@结束
MapViewController.m

#import "MapViewController.h"
@import CoreLocation;

@interface MapViewController ()<CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;

@end

@implementation MapViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    defaults = [NSUserDefaults standardUserDefaults];
    reportedSightings = [[NSMutableArray alloc] init];


    [reportedSightings addObject:@"turtle1,44.008897,-77.743073"];
    [reportedSightings addObject:@"turtle2,43.997620,-77.675193"];
    [reportedSightings addObject:@"turtle3,44.001554,-77.689685"];
    [reportedSightings addObject:@"turtle4,43.992655,-77.712643"];
    [reportedSightings addObject:@"turtle5,44.005708,-77.725666"];

    [reportedSightings addObject:@"snake1,43.993950,-77.720637"];
    [reportedSightings addObject:@"snake2,43.994176,-77.717224"];
    [reportedSightings addObject:@"snake3,43.998308,-77.677515"];
    [reportedSightings addObject:@"snake4,44.007523,-77.719716"];
    [reportedSightings addObject:@"snake5,43.997320,-77.731911"];
    [reportedSightings addObject:@"snake6,43.995390,-77.716450"];

    [reportedSightings addObject:@"amphibian1,43.996503,-77.694154"];
    [reportedSightings addObject:@"amphibian2,43.989638,-77.704582"];
    [reportedSightings addObject:@"amphibian3,44.009406,-77.738130"];
    [reportedSightings addObject:@"amphibian4,44.001059,-77.733429"];
    [reportedSightings addObject:@"amphibian5,44.005405,-77.713410"];
    [reportedSightings addObject:@"amphibian6,44.002750,-77.699245"];
    [reportedSightings addObject:@"amphibian7,43.999160,-77.692886"];
    [reportedSightings addObject:@"amphibian8,43.993869,-77.722742"];
    [reportedSightings addObject:@"amphibian9,43.995589,-77.714681"];
    [reportedSightings addObject:@"amphibian10,43.998462,-77.675608"];


    [defaults setObject:reportedSightings forKey:@"reportedSightings"];
    [defaults synchronize];
    reportedSightings = [NSMutableArray arrayWithArray:[defaults objectForKey:@"reportedSightings"]];



    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        [self.locationManager requestWhenInUseAuthorization];

    [self.locationManager startUpdatingLocation];
    self.mapView.showsUserLocation = YES;

    MKCoordinateRegion region = {{0.0, 0.0}, {0.0, 0.0}};
    region.center.latitude = 43.998564;
    region.center.longitude = -77.709888;

    [self.mapView setRegion:region];



    for(int i=0; i<reportedSightings.count; i++)
    {
        NSString *reportedSighting = reportedSightings[i];
        NSString *speciesName = [reportedSighting substringToIndex:[reportedSighting rangeOfString:@","].location];
        reportedSighting = [reportedSighting substringFromIndex:[reportedSighting rangeOfString:@","].location+1];
        NSString *sightingLatitude = [reportedSighting substringToIndex:[reportedSighting rangeOfString:@","].location];
        reportedSighting = [reportedSighting substringFromIndex:[reportedSighting rangeOfString:@","].location+1];
        NSString *sightingLongitude = reportedSighting;


        float latitude = [sightingLatitude floatValue];
        float longitude = [sightingLongitude floatValue];
        CLLocationCoordinate2D reportedSightingCoordinates = CLLocationCoordinate2DMake(latitude, longitude);


        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = reportedSightingCoordinates;
        point.title = speciesName;
        point.subtitle = [NSString stringWithFormat:@"%f, %f", latitude, longitude];
        [self.mapView addAnnotation:point];

    } 
}



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([self.mapView respondsToSelector:@selector(camera)])
    {
        MKMapCamera *newCamera = [[self.mapView camera] copy];

        [newCamera setPitch:0.0];
        [newCamera setHeading:307.197710];
        [newCamera setAltitude:14515.983058];

        [self.mapView setCamera:newCamera animated:YES];
    }
}


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

    if([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        MKAnnotationView *pinView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomViewAnnotation"];

        if(!pinView)
        {
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomViewAnnotation"];
            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
        }

        else
        {
            pinView.annotation = annotation;
        }

        return pinView;
    }

    return nil;
}


@end
#导入“MapViewController.h”
@进口货位;
@接口MapViewController()
@属性(强,非原子)CLLocationManager*locationManager;
@结束
@MapViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
默认值=[NSUserDefaults standardUserDefaults];
ReportedSights=[[NSMutableArray alloc]init];
[报告的目击添加对象:@“turtle1,44.008897,-77.743073”];
[报告的目击添加对象:@“turtle2,43.997620,-77.675193”];
[报告的目击添加对象:@“turtle3,44.001554,-77.689685”];
[报告的目击添加对象:@“turtle4,43.992655,-77.712643”];
[报告的目击添加对象:@“turtle5,44.005708,-77.725666”];
[报告的目击添加对象:@“snake1,43.993950,-77.720637”];
[报告的目击添加对象:@“snake2,43.994176,-77.717224”];
[报告的目击添加对象:@“snake3,43.998308,-77.677515”];
[报告的目击添加对象:@“snake4,44.007523,-77.719716”];
[报告的目击添加对象:@“snake5,43.997320,-77.731911”];
[报告的目击添加对象:@“snake6,43.995390,-77.716450”];
[报告的目击添加对象:@“两栖动物1,43.996503,-77.694154”];
[报告的目击添加对象:@“两栖动物2,43.989638,-77.704582”];
[报告的目击添加对象:@“两栖动物3,44.009406,-77.738130”];
[报告的目击添加对象:@“两栖动物4,44.001059,-77.733429”];
[报告的目击添加对象:@“两栖动物5,44.005405,-77.713410”];
[报告的目击添加对象:@“两栖动物6,44.002750,-77.699245”];
[报告的目击添加对象:@“两栖动物7,43.999160,-77.692886”];
[报告的目击添加对象:@“两栖动物8,43.993869,-77.722742”];
[报告的目击添加对象:@“两栖动物9,43.995589,-77.714681”];
[报告的目击添加对象:@“两栖动物10,43.998462,-77.675608”];
[默认设置对象:reportedSightings forKey:@“reportedSightings”];
[默认同步];
reportedSightings=[NSMutableArray arrayWithArray:[默认值objectForKey:@“reportedSightings”];
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
//检查iOS 8。如果没有此防护,代码将在iOS 7上因“未知选择器”而崩溃。
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
[self.locationManager在未使用授权时请求];
[self.locationManager startUpdatingLocation];
self.mapView.showsUserLocation=是;
MKCoordinateRegion={0.0,0.0},{0.0,0.0};
region.center.lation=43.998564;
region.center.longitude=-77.709888;
[self.mapView setRegion:region];
对于(int i=0;i对于本节:

    if(!pinView)
    {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomViewAnnotation"];
        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
    }

    else
    {
        pinView.annotation = annotation;
    }
添加行:

pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
对于else也是如此,因此它将如下所示:

if(!pinView)
    {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomViewAnnotation"];
        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
    }

    else
    {
        pinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@Annotation", [annotation title]]];
        pinView.annotation = annotation;
    }

pin图像相互混合,或者您是否看到其他图像/空图像?我不确定我是否理解您的要求。没有“空图像”。最初,所有图像都在正确的注释上。只有当我浏览地图,然后返回注释时,它们似乎会更改图像。但是,当它们刷新时,没有图像是空白的…它们只是不同而已。