自定义MKAnnotationView与框架,图标和图像

我一直在寻找一个类似的解决scheme,如何devise一个像这样的自定义MKAnnotationView。

在这里输入图像说明

我分类了MKAnnotation,我能够添加一个名为F.png的图像F.png是一个帧图像,如图所示。 我想要的是添加一个内部图像。 (我画的彩色蓝色)

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)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 = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"]]; UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside]; [rightButton setTitle:annotation.title forState:UIControlStateNormal]; annotationView.rightCalloutAccessoryView = rightButton; annotationView.canShowCallout = YES; annotationView.draggable = NO; return annotationView; } return nil; } 

在你的代码中

 else { MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]; annotationView.canShowCallout = YES; //change here annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"]]; UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"]; UIImage *image = theImageInFrameInner; UIGraphicsBeginImageContext(CGSizeMake(pin.size.width, pin.size.height)); [frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)]; [image drawInRect:CGRectMake(2, 2, 60, 60)]; // the frame your inner image //maybe you should draw the left bottom icon here, //then set back the new image, done annotationView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside]; [rightButton setTitle:annotation.title forState:UIControlStateNormal]; annotationView.rightCalloutAccessoryView = rightButton; annotationView.canShowCallout = YES; annotationView.draggable = NO; return annotationView; } 
 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *AnnotationViewID = @"annotationViewID"; MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if (annotationView == nil) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; } UIImage *imgPinBorder = [UIImage imageNamed:@"pinBorder.png"]; UIImageView *imageViewPinBorder = [[UIImageView alloc] initWithImage:imgPinBorder]; imageViewPinBorder.center = annotationView.center; [annotationView addSubview:imageViewPinBorder]; UIImage *img = [UIImage imageNamed:@"myIcon.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:img]; imageView.center = annotationView.center; [annotationView addSubview:imageView]; annotationView.annotation = annotation; UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]; [rightButton setTitle:annotation.title forState:UIControlStateNormal]; annotationView.rightCalloutAccessoryView = rightButton; annotationView.canShowCallout = YES; annotationView.draggable = NO; return annotationView; }