Search code examples
iosobjective-cmacosuiimagensimage

Get NSRunningApplication's icon with an other size


// 561 is PID of Chrome application in Activity Monitor.
NSRunningApplication *chromeApp = [NSRunningApplication runningApplicationWithProcessIdentifier:561];
self.imgView.image = chromeApp.icon;

My log:

(lldb) po chromeApp.icon
<NSImage 0x608000268c40 Size={32, 32} Reps=(
    "<NSIconRefImageRep:0x600000082df0 iconRef=0x103 size:128x128 pixels:128x128>",
    "<NSIconRefImageRep:0x600000083020 iconRef=0x103 size:128x128 pixels:256x256>",
    "<NSIconRefImageRep:0x600000082fd0 iconRef=0x103 size:256x256 pixels:256x256>",
    "<NSIconRefImageRep:0x600000083070 iconRef=0x103 size:256x256 pixels:512x512>",
    "<NSIconRefImageRep:0x6000000830c0 iconRef=0x103 size:512x512 pixels:512x512>",
    "<NSIconRefImageRep:0x600000083110 iconRef=0x103 size:48x48 pixels:48x48>",
    "<NSIconRefImageRep:0x600000083160 iconRef=0x103 size:36x36 pixels:36x36>",
    "<NSIconRefImageRep:0x600000083200 iconRef=0x103 size:36x36 pixels:72x72>",
    "<NSIconRefImageRep:0x600000083250 iconRef=0x103 size:32x32 pixels:32x32>",
    "<NSIconRefImageRep:0x6000000832a0 iconRef=0x103 size:32x32 pixels:64x64>",
    "<NSIconRefImageRep:0x6000000832f0 iconRef=0x103 size:18x18 pixels:18x18>",
    "<NSIconRefImageRep:0x6000000831b0 iconRef=0x103 size:18x18 pixels:36x36>",
    "<NSIconRefImageRep:0x600000083340 iconRef=0x103 size:16x16 pixels:16x16>",
    "<NSIconRefImageRep:0x600000083390 iconRef=0x103 size:16x16 pixels:32x32>",
    "<NSIconRefImageRep:0x6000000833e0 iconRef=0x103 size:512x512 pixels:1024x1024>"
)>

In above code, I is received a image with size 32x32 (in my xib file, imageView's size la 512 x 512). Question: How to get image with 512x512 pixel.


Solution

  • the icon you get back is a NSImage that has ITS size set to 32x32. Means that if you draw it now, it will choose an appropriate NSImageRepresentation. From the Log you can see it has many different ones - they are the perse data

    so all you need to do:

    self.imgView.image.size = NSMakeSize(512,512);

    an NSImage is basically just a 'drawable' that has no own data but 1..n representations of image data that it 'groups' together