Hi Squ, 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; CGFloat _margin; } - (id)initWithBaseImage:(NSImage *)baseImage margin:(CGFloat)margin; @property (readonly) NSImage *baseImage; @property (readonly) CGFloat margin; @end @implementation MarginalImageRep - (id)initWithBaseImage:(NSImage *)baseImage margin:(CGFloat)margin { NSParameterAssert(baseImage != nil); self = [super init]; if (self) { { _baseImage = [baseImage retain]; _margin = margin; NSSize sizeIncludingMargin = [baseImage size]; sizeIncludingMargin.width += 2*margin; sizeIncludingMargin.height += 2*margin; [self setSize:sizeIncludingMargin]; } } return self; } -(void)dealloc { [_baseImage release]; [super dealloc]; } - (BOOL)draw { NSRect bounds = (NSRect){NSZeroPoint, [self size]}; [_baseImage drawInRect:NSInsetRect(bounds, _margin, _margin) fromRect: NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; return YES; } - (id)copyWithZone:(NSZone *)zone { MarginalImageRep *rep = [super copyWithZone:zone]; // careful - superclass uses NSCopyObject. rep->_baseImage = [_baseImage retain]; rep->_margin = _margin; return rep; } @synthesize baseImage=_baseImage, margin=_margin; @end We covered this technique in the WWDC 2009 talk about NSImage. If you have access, I'd recommend watching the presentation when it comes out. It explains some of the choices here. You'd use the image like this: MarginalImageRep *marginalRep = [[[MarginalImageRep alloc] initWithBaseImage:baseImage margin:20] autorelease]; NSImage *marginalImage = [[[NSImage alloc] initWithSize:[marginalRep size]] autorelease]; [marginalImage addRepresentation:marginalRep]; // it's a waste of resources to cache here, because the baes image can maintain its own cache. [marginalImage setCacheMode:NSImageCacheNever]; -Ken On Thu, Sep 17, 2009 at 7:39 PM, Squ Aire <squ...@live.com> wrote: > > Introduction: I have an actual ICNS file with some nice icon in it. No > matter where I draw it within my app, be it a 16x16 square or a 512x512 > square, it draws nicely, scales properly, looks crisp at all times. It does > this probably (among other things) by using the correct "representation" > within the ICNS file to determine which size in that file it should use to > do the actual drawing. So far so good. > > > My problem is this: I want to derive a new "icon file" (simulated by an > NSImage somehow) which has some margins applied to it. The margins on each > side should be the size of the area being drawn on divided by 100. So for > instance, if we are drawing the icon on a 300x300 area, the > top,bottom,left,right margins should all be 3. If drawing on a 30x30 area > the margins are .3 or simply negligible. > > > In other words, I somehow want to make an instance of NSImage such that if > drawn on a 300x300 rect, it will look slightly smaller than that because of > the margins, i.e. it will not fill out the whole rect as the original ICNS > would. > > > But also note that this new derived "ICNS file" (simulated by NSImage) > should of course also draw properly when drawn on a, say, 20x20 square. In > other words, the new simulated ICNS file (NSImage instance) should also be > able to have those original size "representations" in it somehow that the > drawing system can choose from to draw correctly at the requested size. > > > How would you accomplish this? I cannot really make an NSImage, "lock > focus" on it and draw on it beforehand like usual, because I cannot know for > sure in the beginning at which size the image will be drawn at. Any ideas? > > > --. > _________________________________________________________________ > Windows Liveā¢: Keep your life in sync. Check it out! > > http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t1_allup_explore_012009_______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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: > http://lists.apple.com/mailman/options/cocoa-dev/kenferry%40gmail.com > > This email sent to kenfe...@gmail.com > _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com