开发者

Get the full size icons of the Trash can

I am trying to display the icons of the Trash can in my app, both empty and full. I've tried several methods to get the icons, but each time the 开发者_如何转开发size is 32x32. Do you know a way to get a full size image?


I assume that you’re getting the trash icon through NSWorkspace, using the kTrashIcon constant from IconsCore.h (if you’re not, this is :

NSImage* image = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kTrashIcon)];

…this NSImage contains representations in a number of different sizes. If you want the biggest one, just iterate through the available representations to find it:

NSEnumerator* representationEnumerator = [[image representations] objectEnumerator];
NSSize biggestSize = NSMakeSize(0, 0);
NSSize size;
while ((size = [(NSImageRep*)[representationEnumerator nextObject] size]).width) {
    if (size.width > biggestSize.width) {
        biggestSize = size;
    }
}
[image setSize:biggestSize];

…On my computer, this results in an NSImage set to 512x512.


If you're creating an image from the following file, then realize that 32px x 32px is simply the default size:

/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/TrashIcon.icns

Just resize it to the size you'd like:

NSString *path = @"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/TrashIcon.icns";
NSImage *image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
[image setSize:NSMakeSize(512.0, 512.0)];

When you do so, the NSImage will automatically choose the appropriate image rep from those that are available. For example, logging the image shows the following description:

image == NSImage 0x102e16bc0 Size={512, 512} Reps=(

    "NSBitmapImageRep 0x102e1f650 Size={512, 512} ColorSpace=(not yet loaded)
      BPS=8 BPP=(not yet loaded) Pixels=512x512 Alpha=YES Planar=NO
  Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x102e187d0",

    "NSBitmapImageRep 0x102e24c80 Size={256, 256} ColorSpace=(not yet loaded)
     BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO
  Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x102e187d0",

    "NSBitmapImageRep 0x102e25540 Size={128, 128} ColorSpace=(not yet loaded)
    BPS=8 BPP=(not yet loaded) Pixels=128x128 Alpha=YES Planar=NO
  Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x102e187d0",

    "NSBitmapImageRep 0x102e25e30 Size={32, 32} ColorSpace=(not yet loaded)
    BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO 
  Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x102e187d0",

    "NSBitmapImageRep 0x102e26720 Size={16, 16} ColorSpace=(not yet loaded)
    BPS=8 BPP=(not yet loaded) Pixels=16x16 Alpha=YES Planar=NO
  Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x102e187d0"
)

Most images that represent file icons such as those returned by NSWorkspace will have many sizes available, though 32 x 32 is the default size.


/System/Library/CoreServices/Dock.app/Contents/Resources/trashfull.png /System/Library/CoreServices/Dock.app/Contents/Resources/trashempty.png

It's 128x128 icon


Search "Mac OS Trash" on Google Images

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜