Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Plotting icons into NSImage



On Wed, 2 May 2007 23:06:10 +0200, Daniel Dalquen <email@hidden>
said:
>
>On 02.05.2007, at 21:54, Uli Kusterer wrote:
>
>>  Just a guess: Have you tried doing an NSRectFill() with clearColor
>> (transparentColor? whatever...) before drawing? It might just be
>> that the image starts out opaque and when you draw it just
>> composites on that?
>
>Well, what I finally ended up doing was compositing two images
>together - the icon ontop of a white square.
>
>> Otherwise, is there any reason why you can't use NSWorkspace's
>> iconForFile: ?
>
>Yes: I like to write a lot of unnecessary code :P
>
>It appears that the icons are rendered slightly nicer with the C-
>Code, but there are problems for small icon sizes (no idea why). Is
>there a better way of resizing the NSImage received from NSWorkspace
>than setSize on the image?

setSize, using a standard icon size (e.g. 16 by 16), does exactly the right
thing. It does not scale; it uses the existing icon representation at that
size. However, if the icon is incorrectly constructed so that it lacks a 16
by 16 representation, then yes, you'll get a somewhat fuzzy result.

You can compensate in that situation using code like the following, but be
warned, if you have a lot of icons to fetch and draw, it is prohibitively
expensive:

            icon = [[NSWorkspace sharedWorkspace]
iconForFile:(NSString*)path];
            NSSize tinySize = NSMakeSize(16.0, 16.0);
            [icon setSize: tinySize];

            NSEnumerator* e = [[icon representations] objectEnumerator];
            NSImageRep* rep;
            BOOL has16 = NO;
            while ((rep = [e nextObject])) {
                if (NSEqualSizes([rep size], tinySize)) {
                    has16 = YES; break;
                }
            }
            if (!has16) {
                NSImage* newIcon = [[NSImage alloc]
initWithSize:NSMakeSize(16.0, 16.0)];
                [newIcon lockFocus];
                [[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationHigh];
                [icon drawInRect:NSMakeRect(0,0,16,16)
fromRect:NSMakeRect(0,0, [icon size].width, [icon size].height)
operation:NSCompositeCopy fraction:1.0];
                [newIcon unlockFocus];
                icon = [newIcon autorelease];
            }

m.
-- 
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.