2013-10-23 46 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 –