views:

780

answers:

1

When using NSImage's imageNamed: method to get the icon for the current computer, if I'm running on a Mac Mini, then I get a low resolution image. If I run the same code from my MacBook, then I get a high-res icon like I'd expect.

My code is as follows:

NSImage *image;
image = [NSImage imageNamed:@"NSComputer"];
[image setSize: NSMakeSize(512,512)];
NSData  * tiffData = [image TIFFRepresentation];
NSBitmapImageRep *bitmap = [NSBitmapImageRep imageRepWithData:tiffData];
data = [bitmap representationUsingType:NSPNGFileType properties:nil];
mime = @"image/png";

When I run that on my MacBook, everything works great and I get a 512x512 icon of the my computer. When I run it on my Mac Mini, i get a 32x32 icon that's been upscaled to 512x512.

Any ideas on how I can get the high-res version?

+2  A: 

I believe that image comes from /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/

The Mac mini icon is named com.apple.macmini.icns. You may want to check if that file contains a 512x512 icon on your machine (it does on mine, running Snow Leopard)'

For debugging purposes, you should also send the output of -representations to NSLog to verify that the icon does or does not have a 512x512 version.

Edit:

The 10.6 AppKit Release Notes has quite a bit on the updated relationship between NSImage and CGImage.

On Snow Leopard, you can get the full resolution image by converting to a CGImageRef:

NSImage* image = [NSImage imageNamed:NSImageNameComputer];
NSRect imageRect = NSMakeRect(0, 0, 512, 512);
CGImageRef cgImage = [image CGImageForProposedRect:&imageRect context:nil hints:nil];

From there, you can create a new NSImage from the CGImageRef, or use the CGImageDestination* APIs to write a png file.

Darren
The unfortunate thing about this approach is that it requires the programmer to hard-code in paths and names of icon files.
Dave DeLong
@Dave DeLong, I believe he means that the image "NSComputer" is fetched by the system from the directory...and he mention it here so you can check for yourself what reps are in the actual file. Not that you should put the full path in your code.
epatel
more clues:1. the macbook (where it works) is running leopard, and the mini (where it's broken) is running snow leopard2. if i use an NSImageView to show the NSComputer icon, it does display at full res, so the file is there in the system - i just can't seem to get an NSImage object at full res3. when i create an [NSImage imageNamed:], it only has 1 representation and it has a size of 32
adam.wulf
It's a Snow Leopard thing. If you load the image file directly from the path you get all representations. If you load it using +imageNamed: you only get the 32x32 representation; however, if you then pass that image instance to NSImageView it can display the full-resolution image. Maybe [NSImage imageNamed:] is returning some cached image instance with the other representations removed?
Darren
works like a charm!
adam.wulf