MKAnnotationView 无法正常工作
MKAnnotationView not working properly
我正在尝试根据对象值替换 MKAnnotationView 图像,代码已执行但图像是随机分配的,因此它们与正确的值不匹配。
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(EventAnnotation *)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.image = [self setImageByType:annotation];
annotationView.draggable = NO;
return annotationView;
}
return nil;
}
setImageByType 方法:
- (UIImage*)setImageByType:(EventAnnotation *)annotation{
//Set annotation image depending on its type
UIImage *image = [[UIImage alloc]init];
NSString *type = annotation.type;
if ([type isEqualToString:@"event"]) {
image = [UIImage imageNamed:@"geolocalization_events"];
}else if ([type isEqualToString:@"show"]){
image = [UIImage imageNamed:@"geolocalization_shows"];
}else if ([type isEqualToString:@"culture"]){
image = [UIImage imageNamed:@"geolocalization_culture"];
}else if ([type isEqualToString:@"eat"]){
image = [UIImage imageNamed:@"geolocalization_eat"];
}else if ([type isEqualToString:@"drink"]){
image = [UIImage imageNamed:@"geolocalization_drink"];
}else if ([type isEqualToString:@"cybercafe"]){
image = [UIImage imageNamed:@"geolocalization_cybercafe"];
}
UIImage *finalImage = [self imageWithImage:image scaledToSize:CGSizeMake(50, 50)];
return finalImage;
}
调整图像大小:
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//Resize annotation image
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
我会说代码的执行速度比图像分配给视图的速度快。有什么想法吗?
选项 1
尝试更改此行:
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
为此:
NSString* AnnotationIdentifier = annotation.type;
选项 2:
这一行之后:
MKAnnotationView *annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
添加这一行:
annotationView.image = [self setImageByType:annotation];
我正在尝试根据对象值替换 MKAnnotationView 图像,代码已执行但图像是随机分配的,因此它们与正确的值不匹配。
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(EventAnnotation *)annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.image = [self setImageByType:annotation];
annotationView.draggable = NO;
return annotationView;
}
return nil;
}
setImageByType 方法:
- (UIImage*)setImageByType:(EventAnnotation *)annotation{
//Set annotation image depending on its type
UIImage *image = [[UIImage alloc]init];
NSString *type = annotation.type;
if ([type isEqualToString:@"event"]) {
image = [UIImage imageNamed:@"geolocalization_events"];
}else if ([type isEqualToString:@"show"]){
image = [UIImage imageNamed:@"geolocalization_shows"];
}else if ([type isEqualToString:@"culture"]){
image = [UIImage imageNamed:@"geolocalization_culture"];
}else if ([type isEqualToString:@"eat"]){
image = [UIImage imageNamed:@"geolocalization_eat"];
}else if ([type isEqualToString:@"drink"]){
image = [UIImage imageNamed:@"geolocalization_drink"];
}else if ([type isEqualToString:@"cybercafe"]){
image = [UIImage imageNamed:@"geolocalization_cybercafe"];
}
UIImage *finalImage = [self imageWithImage:image scaledToSize:CGSizeMake(50, 50)];
return finalImage;
}
调整图像大小:
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//Resize annotation image
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
我会说代码的执行速度比图像分配给视图的速度快。有什么想法吗?
选项 1
尝试更改此行:
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
为此:
NSString* AnnotationIdentifier = annotation.type;
选项 2:
这一行之后:
MKAnnotationView *annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
添加这一行:
annotationView.image = [self setImageByType:annotation];