获取垃圾箱的完整尺寸图标(Get the full size icons of the Trash can)

我试图在我的应用程序中显示垃圾桶的图标,包括空的和完整的。 我已经尝试了几种方法来获取图标,但每次大小为32x32。 你知道一种获得全尺寸图像的方法吗?

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?

最满意答案

我假设您使用来自IconsCore.h的kTrashIcon常量通过NSWorkspace获取垃圾图标(如果您不是,则为:

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

...这个NSImage包含许多不同大小的表示。 如果你想要最大的一个,只需遍历可用的表示来找到它:

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];

...在我的计算机上,这会导致NSImage设置为512x512。

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.

更多推荐