获取plist中的图像路径

发布于 2024-12-03 21:16:13 字数 2435 浏览 0 评论 0原文

我正在尝试获取存储在 plist 中的图像路径(例如,annotation1.jpg)以放入自定义标注,具体取决于它是哪个 POI。 现在,我只获取一张静态图像。

我如何在我的代码中实现它?

   - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
        if (annotation == self.calloutAnnotation) {
            CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"];
            if (!calloutMapAnnotationView) {
                calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation 
                                                                                 reuseIdentifier:@"CalloutAnnotation"] autorelease];
                calloutMapAnnotationView.contentHeight = 78.0f;
                UIImage *asynchronyLogo = [UIImage imageNamed:@"something.png"];
                UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease];
                asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height);
                [calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];
            }
            calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
            calloutMapAnnotationView.mapView = self.mapView;
            return calloutMapAnnotationView;
        } else if ([annotation isKindOfClass:[MyHomeAnnotation class]]) {
            MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                                   reuseIdentifier:@"CustomAnnotation"] autorelease];
            annotationView.canShowCallout = NO;
            annotationView.pinColor = MKPinAnnotationColorGreen;
            return annotationView;
        }else if ([annotation isKindOfClass:[MyMapAnnotation class]]) {
            MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                                   reuseIdentifier:@"NormalAnnotation"] autorelease];
            annotationView.canShowCallout = NO;
            annotationView.pinColor = MKPinAnnotationColorRed;
            return annotationView;
        }


        return nil;
    }

感谢您的帮助!

I am trying to fetch an image path (eg. annotation1.jpg ) stored in a plist to put in a custom callout, depending on which POI it is.
Right now, I am fetching only one static image.

How can I implement that in my code?

   - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
        if (annotation == self.calloutAnnotation) {
            CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutAnnotation"];
            if (!calloutMapAnnotationView) {
                calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation 
                                                                                 reuseIdentifier:@"CalloutAnnotation"] autorelease];
                calloutMapAnnotationView.contentHeight = 78.0f;
                UIImage *asynchronyLogo = [UIImage imageNamed:@"something.png"];
                UIImageView *asynchronyLogoView = [[[UIImageView alloc] initWithImage:asynchronyLogo] autorelease];
                asynchronyLogoView.frame = CGRectMake(5, 2, asynchronyLogoView.frame.size.width, asynchronyLogoView.frame.size.height);
                [calloutMapAnnotationView.contentView addSubview:asynchronyLogoView];
            }
            calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
            calloutMapAnnotationView.mapView = self.mapView;
            return calloutMapAnnotationView;
        } else if ([annotation isKindOfClass:[MyHomeAnnotation class]]) {
            MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                                   reuseIdentifier:@"CustomAnnotation"] autorelease];
            annotationView.canShowCallout = NO;
            annotationView.pinColor = MKPinAnnotationColorGreen;
            return annotationView;
        }else if ([annotation isKindOfClass:[MyMapAnnotation class]]) {
            MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                                                                                   reuseIdentifier:@"NormalAnnotation"] autorelease];
            annotationView.canShowCallout = NO;
            annotationView.pinColor = MKPinAnnotationColorRed;
            return annotationView;
        }


        return nil;
    }

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2024-12-10 21:16:13

您可以将 PLIST 加载到 NSDictionary 中:

NSDictionary *myDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathAndFileName];

一旦有了字典,您就可以使用以下命令提取所需的密钥:

NSString *imageFileName = [myDictionary objectForKey:@"imageFileName"];

然后您可以使用以下命令创建图像(只要它在捆绑包中):

UIImage *image = [UIImage imageNamed:imageFileName];

You can load a PLIST into an NSDictionary:

NSDictionary *myDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathAndFileName];

Once you have the dictionary, you can pull out the key you need with:

NSString *imageFileName = [myDictionary objectForKey:@"imageFileName"];

You can then create the image just as you have (as long as it is in the bundle) with:

UIImage *image = [UIImage imageNamed:imageFileName];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文