macOS:确定从 iconForFile 方法返回的图标的实际大小

macOS: Determine real size of icon returned from iconForFile method

我想要一种方法来判断文件图标是否仅提供 32 x 32 图像,或者它是否具有现代大图标尺寸 (512 x 512),以便我知道在放大时使用哪种高档方法。

我发现许多跨平台应用程序仍然只提供 32 x 32 大小的 Mac 图标,放大到更大尺寸时往往看起来很糟糕,除非使用最近邻方法进行放大。另一方面,现代的大图标在与最近的邻居一起放大时往往看起来很糟糕。在我的 QuickLook 扩展中,我使用 [[NSWorkspace sharedWorkspace] iconForFile:path] 获取图标图像,它总是 returns 大小为 32 x 32 的 NSImage,但没有提供实际最大可用图标大小的线索。

如果我遍历它的图像表示,最大的总是 2048 x 2048 像素。

有办法查明吗?

您需要检查图像表示。如果是 NSPDFImageRep 你可以调用 [image setSize:NSMakeSize(512,512)];

获得最大位图表示的最简单解决方案:

NSImageRep *imgRep = [image bestRepresentationForRect:NSMakeRect(0, 0, 2048, 2048) context:nil hints:nil];
NSLog(@"%@",NSStringFromSize([imgRep size]));

或者检查所有给定的位图表示。

NSURL *URL;
CFErrorRef error;
URL = (__bridge_transfer NSURL *)LSCopyDefaultApplicationURLForContentType(kUTTypeVCard, kLSRolesViewer, &error);
NSImage *image = [[NSWorkspace sharedWorkspace] iconForFile:[URL path]];
NSArray<NSImageRep *>*representations = [image representations];
NSUInteger indexOfLargest = 0;
NSUInteger surfaceArea = 0;
for (NSUInteger i = 0 ; i < [representations count]; i++) {
    NSImageRep *rep = representations[i];
    if ([rep size].width * [rep size].height >= surfaceArea) {
        surfaceArea = [rep size].width * [rep size].height;
        indexOfLargest = i;
    }
}
NSLog(@"Representations %@", representations);
NSLog(@"Largest index is: %lu", (unsigned long)indexOfLargest);

控制台输出:

2021-07-16 19:02:07.816423+0200 testImages[13756:755791] Representations (
    "NSISIconImageRep 0x600002abe2b0 Size={32, 32} ColorSpace=Generic RGB colorspace BPS=0 Pixels=32x32 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe0d0 Size={32, 32} ColorSpace=Generic RGB colorspace BPS=0 Pixels=32x32 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe1c0 Size={32, 32} ColorSpace=Generic RGB colorspace BPS=0 Pixels=64x64 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe3a0 Size={32, 32} ColorSpace=Generic RGB colorspace BPS=0 Pixels=64x64 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe3f0 Size={16, 16} ColorSpace=Generic RGB colorspace BPS=0 Pixels=16x16 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe440 Size={16, 16} ColorSpace=Generic RGB colorspace BPS=0 Pixels=16x16 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe490 Size={16, 16} ColorSpace=Generic RGB colorspace BPS=0 Pixels=32x32 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe530 Size={16, 16} ColorSpace=Generic RGB colorspace BPS=0 Pixels=32x32 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe580 Size={18, 18} ColorSpace=Generic RGB colorspace BPS=0 Pixels=18x18 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe5d0 Size={18, 18} ColorSpace=Generic RGB colorspace BPS=0 Pixels=18x18 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe620 Size={18, 18} ColorSpace=Generic RGB colorspace BPS=0 Pixels=36x36 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe4e0 Size={18, 18} ColorSpace=Generic RGB colorspace BPS=0 Pixels=36x36 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe670 Size={24, 24} ColorSpace=Generic RGB colorspace BPS=0 Pixels=24x24 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe6c0 Size={24, 24} ColorSpace=Generic RGB colorspace BPS=0 Pixels=24x24 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe710 Size={24, 24} ColorSpace=Generic RGB colorspace BPS=0 Pixels=48x48 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe760 Size={24, 24} ColorSpace=Generic RGB colorspace BPS=0 Pixels=48x48 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe7b0 Size={128, 128} ColorSpace=Generic RGB colorspace BPS=0 Pixels=128x128 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe800 Size={128, 128} ColorSpace=Generic RGB colorspace BPS=0 Pixels=128x128 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe850 Size={128, 128} ColorSpace=Generic RGB colorspace BPS=0 Pixels=256x256 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe8a0 Size={128, 128} ColorSpace=Generic RGB colorspace BPS=0 Pixels=256x256 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe8f0 Size={256, 256} ColorSpace=Generic RGB colorspace BPS=0 Pixels=256x256 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe940 Size={256, 256} ColorSpace=Generic RGB colorspace BPS=0 Pixels=256x256 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abe990 Size={256, 256} ColorSpace=Generic RGB colorspace BPS=0 Pixels=512x512 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abe9e0 Size={256, 256} ColorSpace=Generic RGB colorspace BPS=0 Pixels=512x512 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abea30 Size={512, 512} ColorSpace=Generic RGB colorspace BPS=0 Pixels=512x512 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abea80 Size={512, 512} ColorSpace=Generic RGB colorspace BPS=0 Pixels=512x512 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abead0 Size={512, 512} ColorSpace=Generic RGB colorspace BPS=0 Pixels=1024x1024 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abeb20 Size={512, 512} ColorSpace=Generic RGB colorspace BPS=0 Pixels=1024x1024 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abeb70 Size={1024, 1024} ColorSpace=Generic RGB colorspace BPS=0 Pixels=1024x1024 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abebc0 Size={1024, 1024} ColorSpace=Generic RGB colorspace BPS=0 Pixels=1024x1024 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua",
    "NSISIconImageRep 0x600002abec10 Size={1024, 1024} ColorSpace=Generic RGB colorspace BPS=0 Pixels=2048x2048 Alpha=NO AppearanceName=NSAppearanceNameAqua",
    "NSISIconImageRep 0x600002abec60 Size={1024, 1024} ColorSpace=Generic RGB colorspace BPS=0 Pixels=2048x2048 Alpha=NO AppearanceName=NSAppearanceNameDarkAqua"
)
2021-07-16 19:02:07.854533+0200 testImages[13756:755791] Largest index is: 31

如果您想访问原始图标表示:

    NSImage *image = nil;
    OSErr err;
    
    err = GetIconRefFromTypeInfo(0, 0, CFSTR("pdf"), 0, 0, &iconRef);
    if (err == noErr && iconRef) {
        
        IconFamilyHandle iconFamily = NULL;
        err = IconRefToIconFamily(iconRef, kSelectorAllAvailableData, &iconFamily);
        ReleaseIconRef(iconRef);

        if(err == noErr && iconFamily)
        {
            NSData *data = [NSData dataWithBytes:*iconFamily length:GetHandleSize((Handle)iconFamily)];
            image = [[NSImage alloc] initWithData:data];
        }
    }

控制台:

2021-07-19 13:35:33.716142+0200 testImages2[10434:559291] Representations (
    "NSBitmapImageRep 0x60000016e8b0 Size={512, 512} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=1024x1024 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x600002a3eca0",
    "NSBitmapImageRep 0x60000014bc60 Size={256, 256} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=512x512 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x600002a3eca0",
    "NSBitmapImageRep 0x60000014bdb0 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=0x600002a3eca0",
    "NSBitmapImageRep 0x60000014bf00 Size={128, 128} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=256x256 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x600002a3eca0",
    "NSBitmapImageRep 0x60000014ca10 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=0x600002a3eca0",
    "NSBitmapImageRep 0x600000144000 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=0x600002a3eca0",
    "NSBitmapImageRep 0x6000001441c0 Size={32, 32} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=64x64 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x600002a3eca0",
    "NSBitmapImageRep 0x600000144310 Size={16, 16} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=32x32 Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x600002a3eca0",
    "NSBitmapImageRep 0x6000001443f0 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=0x600002a3eca0",
    "NSBitmapImageRep 0x6000001444d0 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=0x600002a3eca0"
)