2013-10-23 43 views
0

我需要將框架設置爲用作pinAnnotation圖像的圖像。如何將框架設置爲在MapAnnotation上設置的UIImage

隨着這是我添加圖像,

MKAnnotationView* annotationView = [objMapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 
    if(!annotationView) 
    { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation 
                 reuseIdentifier:annotationIdentifier]; 
    } 

    annotationView.image = [UIImage imageNamed:@"rrb.png"] ; 

enter image description here

目前的觀點像這樣...而這個圖像將被用於每個annotationPin。 雖然這個圖像很大,但我如何設置框架,以便它符合我的框架。 這些圖像將通過webService傳遞。

的視圖應該是這樣...(I改變的圖像的尺寸)

enter image description here

enter image description here

上設置註釋的圖像:

UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:joinString]]; 
     [imgV setFrame:CGRectMake(0, 0, 40, 40)]; 
     annotationView.image = imgV.image; 

// join String是包含從webService獲取的圖像的字符串。

+0

圖像是從Web服務動態加載到應用程序還是已經包含在該包中?如果動態加載,則需要在分配圖像屬性之前以編程方式調整圖像大小(例如,請參閱http://stackoverflow.com/a/13041906/467105)。如果已經包含在包中,那麼在添加到包之前,只需手動調整它們的大小(例如,使用預覽)。 – Anna

+0

@AnnaKarenina:圖像正在通過WebService動態加載。我編輯了該問題,以包括相同的代碼。鏈接幫助但沒有爲我工作.. – iCodeAtApple

+0

我不清楚:你不需要和不應該設置框架。您調整圖像大小。另外,如果joinString是「包含從Web服務獲取的圖像的字符串」,則使用imageNamed沒有任何意義。 imageNamed方法不適用於網址。它在應用程序包中查找該圖像。 – Anna

回答

0

mapView delegate有一個名爲viewForAnnotation的方法。您應該能夠簡單地通過訪問它並將其更改爲任何您喜歡的內容來設置註釋視圖的框架。

+0

如果你可以給我一個書面代碼的演示例子。 – iCodeAtApple

+0

正如你所說我做到了這一點......但沒有工作......! [annotationView setFrame:CGRectMake(0,0,10,10)]; – iCodeAtApple

+0

您可以發佈您嘗試的完整代碼嗎? – Adis

0

可以使用以下:

  • (MKAnnotationView *)的MapView:(的MKMapView *)的MapView viewForAnnotation:(ID)註釋{

    MKAnnotationView * annView = [[MKAnnotationView的alloc] initWithAnnotation:註釋reuseIdentifier:nil];

    UIImage * image = [UIImage imageNamed:@「image.png」]; UIImageView * imageView = [[UIImageView alloc] initWithImage:image]; [annView addSubview:imageView]; [imageView發佈];

    MKPinAnnotationView * pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    [annView addSubview:pinView]; [pinView發佈];

    return annView; }

+0

另一種方法是創建你自己的視圖並在MKAnnotationView委託方法中調用該視圖:viewforannotation –

+0

iRD:我確實使用了上面的代碼,但它給了我8個引腳,而不是4個.4個標準註釋和4個自定義註釋。 – iCodeAtApple

+0

@iCodeAtApple - 你必須檢查你在註釋數組中添加了多少註釋,然後添加到mapView –