Re: resolution independent [NSImage drawInRect]
Re: resolution independent [NSImage drawInRect]
- Subject: Re: resolution independent [NSImage drawInRect]
- From: Peter Schart <email@hidden>
- Date: Tue, 19 Sep 2006 08:37:01 -0500
On Sep 19, 2006, at 8:07 AM, Andrew Bush wrote:
Hi all,
Im drawing some arbitary images into a custom view using drawInRect
and specifying the srcRect, reasonably enough some images, with
resolution of 300dpi instead of 72 dpi, are drawing into a smaller
rect than the one I specify.
Clearly I can fix this by calculating a larger srcRect depending on
the resolution of the image I am handling...but I dont really want
to :) is there an easier (read: built in) way of handling images
of varying resolutions?
thanks for any help.
I ran into a similar problem in an NSImageView subclass I'm working
on. After some searching (but perhaps not enough) I didn't find any
"free" way to do it but I did find some code that got me pointed in
the right direction. What it (and I) do is basically check if the
image's stated width and height in # of pixels matches up with the
image rep it opens with and if so it adds a new rep with the adjusted
size. So if the image data says "I'm 1000 pixels wide" but the image
rep says "I'm 220 pixels wide" you know it is being scaled because of
high dpi.
Here's some sample code, apologies for any errors or ugliness but it
seems to work well for my purposes:
NSSize origSize = [anImage size];
NSImageRep *resizedRep = [[[anImage representations] objectAtIndex:0]
copy];
int pw = [resizedRep pixelsWide];
int ph = [resizedRep pixelsHigh];
if (pw != origSize.width) {
[resizedRep setSize:NSMakeSize(pw, ph)];
[anImage addRepresentation:resizedRep];
[anImage setSize:NSMakeSize(pw, ph)];
}
[resizedRep release];
-pete
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden