RE: Making a new "ICNS file" with NSImage
RE: Making a new "ICNS file" with NSImage
- Subject: RE: Making a new "ICNS file" with NSImage
- From: Squ Aire <email@hidden>
- Date: Sun, 4 Oct 2009 06:49:58 +0000
- Importance: Normal
<email@hidden>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
Thanks for the reply=2C
However. I'm having a hard time testing this (i.e. see the difference in ma=
rgins) because whenever I put an NSImageView on the window in IB=2C and in =
code do something like this:
NSImage *baseImage =3D [NSImage imageNamed:@"myGreatIcon.icns"]=3B
[imageViewOutlet setImage:baseImage]=3B
all I end up seeing in the image view is not as big icon as the image view =
is=2C but the size is just restrained to 128x128 it seems. Note: This also =
happens if I set the image directly in IB=3B the icon fills the image view =
properly when viewed in IB=2C however when compiled and run only a 128 vers=
ion is displayed in the actual application's image view.
I'm also having this problem when making a custom NSView subclass and drawi=
ng into it within drawInRect method using [baseImage drawAtRect:...]
What's going on here? Do you happen to have a sample app that actually test=
s your code and clearly proves the difference in margins? Due to the proble=
ms I've mentioned I just cannot test it :(
> Hi Squ=2C
>
> I would recommend subclassing NSImageRep to do this. The new rep subclass=
should retain the original image and draw it slightly inset.
>
> It's very similar to subclassing NSView. It looks something like this:
>
>
>
> @interface MarginalImageRep : NSImageRep {
>
>
> NSImage *_baseImage=3B
>
>
>
> CGFloat _margin=3B
>
>
>
> }
>
>
>
>
> - (id)initWithBaseImage:(NSImage *)baseImage margin:(CGFloat)margin=3B
>
>
>
>
>
> @property (readonly) NSImage *baseImage=3B
>
>
> @property (readonly) CGFloat margin=3B
>
>
>
>
> @end
>
>
>
>
>
>
> @implementation MarginalImageRep
>
>
>
>
> - (id)initWithBaseImage:(NSImage *)baseImage margin:(CGFloat)margin {
>
>
>
> NSParameterAssert(baseImage !=3D nil)=3B
>
>
>
>
>
>
> self =3D [super init]=3B
>
>
> if (self) {
>
>
> {
>
>
> _baseImage =3D [baseImage retain]=3B
>
>
> _margin =3D margin=3B
>
>
>
>
>
> NSSize sizeIncludingMargin =3D [baseImage size]=3B
>
>
>
> sizeIncludingMargin.width +=3D 2*margin=3B
>
>
>
> sizeIncludingMargin.height +=3D 2*margin=3B
>
>
>
>
>
>
> [self setSize:sizeIncludingMargin]=3B
>
>
> }
>
>
> }
>
>
>
>
>
> return self=3B
>
>
> }
>
>
>
>
> -(void)dealloc {
>
>
> [_baseImage release]=3B
>
>
>
> [super dealloc]=3B
>
>
> }
>
>
>
>
>
>
> - (BOOL)draw {
>
>
> NSRect bounds =3D (NSRect){NSZeroPoint=2C [self size]}=3B
>
>
>
> [_baseImage drawInRect:NSInsetRect(bounds=2C _margin=2C _margin) fromRect=
:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]=3B
>
>
>
> return YES=3B
>
>
> }
>
>
>
>
> - (id)copyWithZone:(NSZone *)zone {
>
>
> MarginalImageRep *rep =3D [super copyWithZone:zone]=3B
>
>
>
> // careful - superclass uses NSCopyObject.
>
>
> rep->_baseImage =3D [_baseImage retain]=3B
>
>
>
> rep->_margin =3D _margin=3B
>
>
>
> return rep=3B
>
>
> }
>
>
>
>
> @synthesize baseImage=3D_baseImage=2C margin=3D_margin=3B
>
>
>
>
>
> @end
>
>
> We covered this technique in the WWDC 2009 talk about NSImage. If you hav=
e access=2C I'd recommend watching the presentation when it comes out. It e=
xplains some of the choices here.
>
>
> You'd use the image like this:
>
>
> MarginalImageRep *marginalRep =3D [[[MarginalImageRep alloc] initWithBase=
Image:baseImage margin:20] autorelease]=3B
>
>
>
> NSImage *marginalImage =3D [[[NSImage alloc] initWithSize:[marginalRep si=
ze]] autorelease]=3B
>
>
>
> [marginalImage addRepresentation:marginalRep]=3B
>
>
>
>
>
> // it's a waste of resources to cache here=2C because the baes image can =
maintain its own cache.
>
>
>
> [marginalImage setCacheMode:NSImageCacheNever]=3B
>
>
>
> -Ken
>
> On Thu=2C Sep 17=2C 2009 at 7:39 PM=2C Squ Aire> wrote:
>
>
>
> Introduction: I have an actual ICNS file with some nice icon in it. No ma=
tter where I draw it within my app=2C be it a 16x16 square or a 512x512 squ=
are=2C it draws nicely=2C scales properly=2C looks crisp at all times. It d=
oes this probably (among other things) by using the correct "representation=
" within the ICNS file to determine which size in that file it should use t=
o do the actual drawing. So far so good.
>
>
>
>
>
>
> My problem is this: I want to derive a new "icon file" (simulated by an N=
SImage somehow) which has some margins applied to it. The margins on each s=
ide should be the size of the area being drawn on divided by 100. So for in=
stance=2C if we are drawing the icon on a 300x300 area=2C the top=2Cbottom=
=2Cleft=2Cright margins should all be 3. If drawing on a 30x30 area the mar=
gins are .3 or simply negligible.
>
>
>
>
>
>
> In other words=2C I somehow want to make an instance of NSImage such that=
if drawn on a 300x300 rect=2C it will look slightly smaller than that beca=
use of the margins=2C i.e. it will not fill out the whole rect as the origi=
nal ICNS would.
>
>
>
>
>
>
> But also note that this new derived "ICNS file" (simulated by NSImage) sh=
ould of course also draw properly when drawn on a=2C say=2C 20x20 square. I=
n other words=2C the new simulated ICNS file (NSImage instance) should also=
be able to have those original size "representations" in it somehow that t=
he drawing system can choose from to draw correctly at the requested size.
>
>
>
>
>
>
> How would you accomplish this? I cannot really make an NSImage=2C "lock f=
ocus" on it and draw on it beforehand like usual=2C because I cannot know f=
or sure in the beginning at which size the image will be drawn at. Any idea=
s?
=0A=
_________________________________________________________________=0A=
Windows Live: Keep your friends up to date with what you do online.=0A=
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/so=
cial-network-basics.aspx?ocid=3DPID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092=
010=
_______________________________________________
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