Search code examples
objective-cmacoscocoansscrollviewnsimageview

Wrong postion when plotting an nsiamgeview over an nsscrollview?


I have an NSScrollview which has a large imageview inside it.I am trying to plot an imageview programatically over the imageview inside the nsscrollview. Think of it like this.I have a worldmap(NSImageView) inside an NSScrollView.The user selects a country from the dropdown list and I get the (x,y) points for that country in the worldmap(NSImageview). Now I have to draw a small image(a red pointer) over the worldmap at the given (x,y) points. The problem is that when I draw the image it gets plotted at the wrong area.

    self.pimg = [[NSImageView alloc] initWithFrame:NSMakeRect (0, 0, 30, 30)];
    [self.pimg setImage: image];
//self.pimg is the pointing image to be drawn over the worldmap
    [self.pimg setImageFrameStyle:NSImageFrameNone];
    [self.pimg setImageScaling:NSOnState];
//
    [self.scrollView.contentView addSubview:self.pimg];

I entered (0,0) as x,y to check whether It gets drawn on the origin.It gets drawn somewhere in the middle of the screen.

EDIT: It gets drawn at the origin of the scrollview.How do I translate these points to the large image's points?


Solution

  • Add your image as the subview to your worldmapimage (large image view). This will translate the image points as per your world map image.

     self.pimg = [[NSImageView alloc] initWithFrame:NSMakeRect (0, 0, 30, 30)];
     [self.pimg setImage: image];
    //self.pimg is the pointing image to be drawn over the worldmap
      [self.pimg setImageFrameStyle:NSImageFrameNone];
      [self.pimg setImageScaling:NSOnState];
    
      //Updated line of code
      [self.largeImageView addSubview:self.pimg]; //Add subview to the largeimageview(world map)