0
votes

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?

2
I tried your code, the image get positioned at the origin of scroll view, i.e bottom left corner of the scroll view. Isn't this what you expect?Neha
Since you are adding your image view as a subview to scroll view, the frame is positioned at the origin of scroll view, not the origin of your window.Neha
I want to add it to the origin of the large image(The world map).How do I translate these points to the image's points?zzzzz
check my answer. Add your image as subview to your worldmapimageview instead of scrollviewNeha

2 Answers

1
votes

With the info you have given its hard to say but you could try using the NSView method

- (NSRect)convertRect:(NSRect)aRect toView:(NSView *)aView

So assuming your worldmap is a subview on your scrollview, maybe...

[scrollview convertRect:self.pimg.frame toView:worldmap]

Which will return a frame in the point reference system of the worldmap view.

1
votes

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)