0

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?

zzzzz
  • 1,209
  • 2
  • 18
  • 45
  • 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 Nov 08 '13 at 07:31
  • 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 Nov 08 '13 at 07:35
  • 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 Nov 08 '13 at 08:18
  • check my answer. Add your image as subview to your worldmapimageview instead of scrollview – Neha Nov 08 '13 at 08:32

2 Answers2

1

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)
Neha
  • 1,751
  • 14
  • 36
1

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.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73