2011-08-20 65 views
1

我正在編寫一個應用程序,該應用程序在leftCalloutAccessoryView中顯示來自服務器的縮略圖。在我真正獲得圖像之前,我會顯示一個地標圖像。下載照片後,會調用一種方法,我想更新該圖像而不刪除原始註釋並放置另一個。我只是將圖像從佔位符切換到下載的縮略圖。以下是不起作用的代碼,但如果有人能讓我知道我是否在正確的軌道上,那會很好。在MKAnnotationView中更新圖像

MyAnnotationClass *annotation=[[MyAnnotationClass alloc] initWithCoordinate:item.location]; 
[annotation setItem:item]; 
if(item.title){ 
    annotation.name=item.title; 
} 
else{ 
    [email protected]"no title"; 
} 
[annotation setDescription:[[NSString alloc] initWithFormat:@"Views:%d Likes:%d Comments:%d",item.views,item.likes,item.comments]]; 
[annotation setImage:[[photo.thumb copyWithDimensions: CGSizeMake(32.0, 32.0)] autorelease]]; 

MKAnnotationView *av=[__mapView viewForAnnotation:annotation]; 
UIImageView *imageView=[[UIImageView alloc] initWithImage:annotation.image]; 
imageView.frame=CGRectMake(0.0, 0.0, 32.0, 32.0); 
av.leftCalloutAccessoryView=imageView; 
[annotation release]; 

我一直在這裏尋找相當長的一段時間,但沒有什麼,我能找到將做我所需要的。謝謝

回答

4

如果註釋已經在地圖上並且您想要更新它,請不要創建新註記。

取而代之,在地圖視圖的annotations數組中找到要更新的註釋並更新其屬性。然後在該現有註釋上調用viewForAnnotation:並更新leftCalloutAccessoryView

此外,確保viewForAnnotation委託方法的優點是設置leftCalloutAccessoryView要麼佔位符圖像或通過檢查註釋的屬性(而不是一味的佔位符圖片)的實際圖像邏輯。

This other question有一些示例代碼可能有幫助。