• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSImage scaling and ugliness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSImage scaling and ugliness


  • Subject: Re: NSImage scaling and ugliness
  • From: Matt Neuburg <email@hidden>
  • Date: Thu, 13 Jul 2006 12:55:17 -0700
  • Thread-topic: NSImage scaling and ugliness

On or about 7/10/06 12:01 PM, thus spake "email@hidden"
<email@hidden>:

> Date: Mon, 10 Jul 2006 11:42:13 -0700
> From: "Stephen F. Booth" <email@hidden>
>
> My code for grabbing the icon looks like:
>
> NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:filename];
> [icon setSize:NSMakeSize(16, 16)];
>
> I have tried setting the scalesWhenResized property, with identical
> results.  The icons that aren't pretty are mainly ones that have no
> 16x16 representation, but since Finder can display them I assume
> there is something I'm not doing.

The upshot is that Stephen and I worked out what's going on. It was quite
obvious, but I wasn't clear on it myself, so for the record, here it is.

When you say setSize:, the image does not necessarily scale. It might
contain an NSImageRep for the target size, in which case it will use that.
In the case of a file icon and the size 16x16, this is extremely likely.

But it isn't 100% certain; an icon can be defective. In that case, setSize
will scale, and it will do it badly.

So the way around this is to use setSize if there is a 16x16 rep, but scale
for real in the rare case where there isn't.

Here is some code that I'm now using in my app:

  icon = [[NSWorkspace sharedWorkspace] iconForFile:(NSString*)path];
  NSSize tinySize = NSMakeSize(16.0, 16.0);
  [icon setSize: tinySize];
  // now check to see whether that actually worked
  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/
pantes anthropoi tou eidenai oregontai phusei
AppleScript: the Definitive Guide - Second Edition!
http://www.amazon.com/gp/product/0596102119
Take Control of Word 2004, Tiger, and more -
http://www.takecontrolbooks.com/tiger-customizing.html
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/



 _______________________________________________
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

  • Prev by Date: Re: initializers in Categories?
  • Next by Date: Re: NSEvent -keyCode decoder ring?
  • Previous by thread: Re: NSImage scaling and ugliness
  • Next by thread: Re: NSImage scaling and ugliness
  • Index(es):
    • Date
    • Thread