Re: drawRect: wrong scale in a programmatically created window
Re: drawRect: wrong scale in a programmatically created window
- Subject: Re: drawRect: wrong scale in a programmatically created window
- From: Quincey Morris <email@hidden>
- Date: Fri, 15 Jan 2016 10:15:26 -0800
- Feedback-id: 167118m:167118agrif8a:167118sOj-s-pjNg:SMTPCORP
On Jan 15, 2016, at 03:42 , Andreas Höschler <email@hidden> wrote:
>
> It seems I have a lack of understanding of the difference of points and pixels.
With “retina” displays, there can be 2 or 3 pixels for each unit of the drawing coordinate system (points). When you did this:
> NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[mapView bounds]];
the bound rect is in view coordinates (points), but what you get back is actually an array of pixels.
> But how does NSBitmapImageRep know which pixel resolution to choose? How does it derive Pixels=1702x1798 when I create the imageRep with a {{0, 0}, {851, 899} rect?
It doesn’t choose. The view/window has a backing store at a pixel resolution that depends on the display device. The image capture API simply grabs all of the pixels inside the 851 x 899 (points) rect, and this happens to be 1792 x 1798 pixels. Yes, the number of pixels you get back will vary according to which Mac you run this on.
> NSImage *image = [[NSImage alloc] initWithData:imageData];
When you create the image, the PNG imageData (I assume) contains the logical (points) bounds of the image you captured, as well as all the actual pixels. As a result, the underlying bitmap image rep has 1792 x 1798 pixels, and the image itself has size {851 x 899}.
This is normally what you want to happen. An image whose point size is 851 x 899 will draw as 851 x 899 points on any device. (The physical size of a point may vary between displays, but that’s a different issue.) The image drawing API has an algorithm for choosing an image rep (from the possibly multiple reps within an image object) and a scaling mechanism.
Thus, if you display the image on the same Mac where you captured it, you’ll get the same pixels displayed as you captured. If you display it on a non-retina Mac (1 pixel per point), the image will be downscaled to 851 x 899 pixels.
Most of this works transparently behind the scenes. However, NSImage ‘size’ property doesn’t always get set automatically to the correct *points* value — depending on how you create the image rep, it might get set to the pixel dimensions, which makes the NSImage size too big. In such cases, you need to set the size property yourself.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden