2011-09-08 175 views
0

我試圖獲取存儲在plist中的圖像路徑(例如,annotation1.jpg)以便放入自定義標註,具體取決於它是哪個POI。 現在,我只提取一張靜態圖片。在plist中獲取圖像路徑

如何在我的代碼中實現該功能?

- (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; 
    } 

謝謝你的幫助!

回答

2

您可以將PLIST加載到一個NSDictionary:

NSDictionary *myDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathAndFileName]; 

一旦你的字典,你可以拉出來,你需要用鑰匙:

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

那麼你可以創建映像因爲你有(只要它在捆綁):

UIImage *image = [UIImage imageNamed:imageFileName]; 
+0

現在,我的代碼看起來像那個 'if(!calloutMapAnnotationView){calloutMapAnnotationView = [[CalloutMapAnnotationView alloc] initWithAnnotation:註釋 reuseIdentifier:@「CalloutAnnotation」] autorelease]; calloutMapAnnotationView.contentHeight = 78.0f; NSDictionary * poiImage = [poiArray objectAtIndex:nextPoiIndex ++]; \t \t \t NSString * imageFileName = [poiImage objectForKey:@「imageFileName」]; UIImage * asynchronyLogo = [UIImage imageNamed:@「imageFileName」]; asynchronyLo' – danskcollignon

+0

但Xcode告訴我imageFileName不是一個使用的變量,即使我正確地合併在我的NSDictionary – danskcollignon

+0

我想你需要通過調試器來運行它,並看到你得到的NSDictionary行,如果你做它創建了非零對象指針。同樣,如果imageFileName在下一行爲零,那麼這意味着該鍵不存在於PLIST中。這可能是鍵名中的拼寫錯誤(它們區分大小寫),或者poiArray沒有正確創建。希望這可以幫助。 –