0
votes

I'm trying to take a screenshot of a view.

I'm using the code at the following link, which is working fine with one exception:

cocoa: how to render view to image?

The problem I have is that if I drag the application window to my second monitor the screen capture grabs the wrong rect. Essentially the rect has been displaced vertically, or is possibly using an origin in the top left rather than bottom left.

The odd thing is that the app works fine on the launch monitor, but when I drag it to the second monitor (without closing and restarting the app) the rect capture goes wrong. If I drag the app back to the launch monitor everything starts working again.

The primary monitor and secondary monitor have different resolutions.

The code that converts the rect is as follows:

NSRect originRect = [aView convertRect:[aView bounds] toView:[[aView window] contentView]];
NSRect rect = originRect;
rect.origin.y = 0;
rect.origin.x += [aView window].frame.origin.x;
rect.origin.y += [[aView window] screen].frame.size.height - [aView window].frame.origin.y - [aView window].frame.size.height;
rect.origin.y += [aView window].frame.size.height - originRect.origin.y - originRect.size.height;

Does anyone know why this is calculating correctly on the launch monitor, but miscalculating on secondary monitors?

The problem must be related to the different resolutions, but I can't see why the call to convertRect:toView (or subsequent calculations) isn't working.

BTW, I'm developing this on 10.8.4 and targeting 10.7.

Thanks

Darren.

1
As ever, the secret to finding a solution to a problem is to research thoroughly, get stumped, post to Stackoverlow, and then you will immediately see the problem... "rect.origin.y += [[aView window] screen].frame.size.height". Will rework and try again.Darren Wheatley

1 Answers

0
votes

The issue was that the screen size was always being taken from the current monitor, when in should have been taken from the primary monitor.

rect.origin.y += [[aView window] screen].frame.size.height - [aView window].frame.origin.y - [aView window].frame.size.height;
rect.origin.y += [aView window].frame.size.height - originRect.origin.y - originRect.size.height;

replaced with:

NSArray *screens = [NSScreen screens];
NSScreen *primaryScreen = [screens objectAtIndex:0];

rect.origin.y = primaryScreen.frame.size.height - [aView window].frame.origin.y - originRect.origin.y - originRect.size.height;

I have added a full answer to the original post I referenced at the top of this one:

cocoa: how to render view to image?