I'd like a way to tell whether a file's icon only provides a 32 x 32 image or if it has modern large icon sizes (512 x 512), so that I know which upscale method to use when enlarging.
I've found that many cross-platform apps still only provide 32 x 32 size Mac icons, which tend to look terrible when blown up to larger sizes, unless upscaled with a nearest neighbour method. On the other hand, modern large icons tend to look terrible when upscaled with nearest neighbour. In my QuickLook extension, I get icon images using [[NSWorkspace sharedWorkspace] iconForFile:path]
which always returns an NSImage of size 32 x 32, but provides no clue to what the actual largest icon size available is.
If I iterate through its image representations, the largest one is always 2048 x 2048 pixels.
Is there a way to find out?
You need to check image representations. If it's NSPDFImageRep
you can just call [image setSize:NSMakeSize(512,512)];
The easiest solution to get largest bitmap representation:
NSImageRep *imgRep = [image bestRepresentationForRect:NSMakeRect(0, 0, 2048, 2048) context:nil hints:nil];
NSLog(@"%@",NSStringFromSize([imgRep size]));
Alternatively check all given bitmap representations.
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);
Console output:
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
If you want to have access to original icon representations:
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];
}
}
Console:
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"
)