在plist中获取图像路径

时间:2011-09-08 16:40:50

标签: iphone ios xcode plist mkannotation

我正在尝试获取存储在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;
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您可以将PLIST加载到NSDictionary:

NSDictionary *myDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathAndFileName];

获得字典后,您可以提取所需的密钥:

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

然后,您可以使用以下内容创建图像(只要它在包中):

UIImage *image = [UIImage imageNamed:imageFileName];