2012-02-25 188 views
0

我在一個UIView子類中有一個MKMapView實例,它通過實現viewForAnnotation:(id<MKAnnotation>) annotation方法符合MKMapViewDelegate協議。其中的代碼是:MKAnnotationView圖像不顯示

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
if ([annotation isKindOfClass:[DriverLocation class]]) { 
    MKAnnotationView* a = [self.map dequeueReusableAnnotationViewWithIdentifier:@"driverView"]; 
    if (a == nil) { 
     MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"]; 
     a.enabled = YES; 
     a.image = [UIImage imageNamed:@"car.png"]; 
    } 
    return a; 
} 
return nil; 

}

圖像沒有正確加載 - 加載的圖像特性具有高度和零寬度和MKAnnotationView實例的尺寸也爲零。

圖像是4Kb png 32像素x 32像素。

我可以確認圖像已被複制到模擬器的根目錄下。

任何幫助,爲什麼這不是加載將不勝感激!

+3

對不起。我是個白癡。我創建了兩個名爲a的變量,在if語句中的實例化應該只是a = ...而不是MKAnnotationView * a。 – Owen 2012-02-25 00:53:31

回答

0

嘗試在設置圖像後添加[a sizeToFit]

0

我以前見過這個。我解決問題的方式是重新導出我的PNG。出於某種原因,他們中的一些人獲得了元數據(比如Fireworks的東西),這導致它在某些情況下出錯。我已經看到這種情況主要發生在Internet Explorer上,但也使用過MKMapView。更奇怪的是,它在模擬器中工作,但不在設備上。

2

爲了完整的這篇文章:您需要更改 MKAnnotationView* a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];

到:

a = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"driverView"];