0

I need to set the frame to an to an image which is used as the image of pinAnnotation.

With This is I add the image,

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

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

enter image description here

The current view is like this...whereas this image is to be used for each annotationPin. Though this image is big but how do i set the frame so that it just fits according to my frame. These images would be coming through webService.

The view is supposed to be like this... (i changed the size of the image)

enter image description here

enter image description here

to set the image on annotation :

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

// join String is the string containing the image fetched from webService.

iCodeAtApple
  • 466
  • 5
  • 16
  • Are the images loaded dynamically in the app from the web service or do you have them already included in the bundle? If loading dynamically, you'll need to resize the image programmatically before assigning to image property (eg. see http://stackoverflow.com/a/13041906/467105). If already included in bundle, then just resize them manually (eg. using Preview) before adding to bundle. –  Oct 23 '13 at 13:52
  • @AnnaKarenina : The images are being loaded dynamically through WebService.I have edited the question to include the code for the same.The linked helped but did not worked for me.. – iCodeAtApple Oct 24 '13 at 05:30
  • I wasn't clear: You do not need to and are not supposed to set the frame. You resize the image. Also, it doesn't make sense that you are using imageNamed if joinString is "a string containing image fetched from web service". The imageNamed method doesn't work with urls. It looks for that image in the app bundle. –  Oct 24 '13 at 10:49
  • @AnnaKarenina :Hey probably you are being mistaken.I fetched the data from webService and after some evaluations i have a text as say, "red.png", and that i collect into joinString and pass the same as image string.And i have a bunch of images that include the images the webService might give.So red.png is already with me.Its just based on some conditions i get an image. Now what i feel like i should get the images resized itself from the designers, rather than resizing it. I dont know if this is the only option.Confused !! – iCodeAtApple Oct 24 '13 at 13:04
  • 1
    Right, so you already have the images in the app bundle. Just load them into Preview or some other graphics utility, resize them (eg. 32 x 32 and 64 x 64 if you also want use the @2x versions), and save them back into the project. No resizing needed at runtime. The fact that you get the _name_ of the image to use from a web service is irrelevant in this case. –  Oct 24 '13 at 13:46
  • @AnnaKarenina : OOps will that work ???? That will be so great !!! – iCodeAtApple Oct 24 '13 at 13:47

2 Answers2

0

The mapView delegate has a method called viewForAnnotation. You should be able to set the frame of your annotation view simply by accessing it and changing it to whatever you like.

Adis
  • 4,512
  • 2
  • 33
  • 40
  • As you Said i did this...but did not work ... !!! [annotationView setFrame:CGRectMake(0, 0, 10, 10)]; – iCodeAtApple Oct 23 '13 at 13:12
  • Adis : Sure ..I have edited the question to include the code for the same – iCodeAtApple Oct 24 '13 at 05:30
  • You're handling the assigning wrong. The image object of imageView has no frame of it's own, and you're passing only that. You should set the last line to be something like this: annotationView = imgV; – Adis Oct 24 '13 at 09:18
  • Adis : I did this ---- annotationView.image = imgV; ---- But this gives me incompatible pointer error. – iCodeAtApple Oct 24 '13 at 13:08
  • That's because you're assigning UIImage to UIImageView. Please read the comment and use annotationView = imgV; – Adis Oct 25 '13 at 08:43
0

You can use the following :

  • (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {

    MKAnnotationView *annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];

    UIImage *image = [UIImage imageNamed:@"image.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [annView addSubview:imageView]; [imageView release];

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

    return annView; }

creative_rd
  • 695
  • 6
  • 13
  • Another method to do this is subclass MKAnnotationview and create your own view and call that view here in MKAnnotationView delegate methods:viewforannotation – creative_rd Oct 23 '13 at 13:22
  • iRD : I did use the above code but it instead gives me 8 pins rather than four..four standard annotations and 4 custom annotations. – iCodeAtApple Oct 23 '13 at 13:29
  • @iCodeAtApple - You have to check how many annotations you are adding in your annotations array and then adding to the mapView – creative_rd Oct 23 '13 at 13:33