objective-c - 通过 url 设置 MKPinAnnotationView 图片

标签 objective-c ios ios6 mapkit

是否可以基于 URL 以编程方式为 MKPinAnnotationView 设置图像?到目前为止我有这个:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *identifier = @"infl8Node";

if ([annotation isKindOfClass:[infl8Node class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.image = [UIImage imageNamed:@"invisible.png"];
    annotationView.animatesDrop = YES;

    //Add image from url
    NSURL *url = [NSURL URLWithString: @"http://cdn2.raywenderlich.com/downloads/arrest.png"];
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
    [annotationView addSubview:imgView];

    return annotationView;
}
return nil;
}

然而它的结果是这样的: img on pin

有没有人做过这个或者对如何完成这个有好的想法?

最佳答案

使用 MKAnnotationView 而不是 MKPinAnnotationView。替换它

 annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        replace use MKAnnotationView here

编辑:一种解决方案是使用自定义 动画

引用MKAnnotationView Animation example 我如何使用自定义动画删除 MKAnnotationViews 的示例。

关于objective-c - 通过 url 设置 MKPinAnnotationView 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449919/

相关文章:

ios - 从 Images.xcassets 加载图像

ios - 方法代码未按正确顺序执行

objective-c - 子类调用父类的方法

objective-c - 如何为 ios6 中的常用短语创建自定义键盘快捷键 #ios

ios - defaultTracker 在 Google Analytics for IOS 中为 nil

ios - 使用 dataWithContentsOfURL : 继续获取 nil 而不是 JSON 文件

ios - 使用 StoreKit 检测应用安装

ios - 在 NSAttributedString 文本下绘制稀松布 ("Blurry Transparent Rectangle")

objective-c - 你如何从 Swift 调用 Objective-C 可变参数方法?

android - 如何以 3D 模式查看 Google map ?