Show an application Icon [Resolved]

Discussion in 'Mac Programming' started by McBgnr, May 14, 2009.

  1. McBgnr macrumors regular

    Joined:
    Apr 13, 2009
    #1
    Hello,

    I was just wondering how Finder shows the applications icons. So I thought of writing a program.

    I am specifying the .app path in my application. And I want to programatically determine the application icon and show it in the Imageview on my application.

    I read through the documentation and found method pathForResource which can be used to determine the path for a resource.

    Also we have the CFBundleIconFile key in the info.plist for applications.

    So I was thinking which method, should be used. Or is there any other better way of doing this?
    Inputs welcome.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You can ask Launch Services for the icon for any file or application. This is the correct way and will return the correct icon. I would look at the LSCopyItemInfoForRef function and the kLSRequestIconAndKind request flag.

    I would note that this is all Carbon and pretty low level. If you are using Cocoa then this is wrapped up for you in the NSWorkspace class. In particular the iconForFile: method.
     
  3. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #3
    Thanks for the quick reply.

    That worked like charm. I used NSWorkspace because I am using cocoa. Any idea how I can display the 16X16 representations of the image.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Not sure what you mean? What do you have right now? If it's bigger than that and you want the 16x16 from the icns file (if there is one) you probably need to drop to Carbon as the NSWorkspace method most likely just returns a single NSImage with the largest/best icon in it. You could just scale the image you have to whatever size you want?
     
  5. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #5
    Sorry for the confusion.

    I printed the contents of NSImage object in the console and it shows:

    Code:
    icon: NSImage 0x185650 Size={32, 32} Reps=(
        NSIconRefBitmapImageRep 0x182960 Size={128, 128} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=32 Pixels=128x128 Alpha=YES Planar=NO Format=0,
        NSIconRefBitmapImageRep 0x182ad0 Size={256, 256} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=32 Pixels=256x256 Alpha=YES Planar=NO Format=0,
        NSIconRefBitmapImageRep 0x182b10 Size={512, 512} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=32 Pixels=512x512 Alpha=YES Planar=NO Format=0,
        NSIconRefBitmapImageRep 0x182b50 Size={32, 32} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=32 Pixels=32x32 Alpha=YES Planar=NO Format=0,
        NSIconRefBitmapImageRep 0x182b90 Size={16, 16} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=32 Pixels=16x16 Alpha=YES Planar=NO Format=0
    ) 
    
    and I also used [image representations] method to get the NSArray of the NSBitmapImageRep and this array has a count of 5. Thus, I am assuming that I can use the image in 5 different sizes, though not sure how :-(
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Well if you can get an array of NSBitmapImageReps then select the one you want (use the size method). Once you have it you can draw it with drawAtPoint: or drawInRect: methods. It really is all in the documentation. I would also note that if you tell the NSImage to draw itself in a 16x16 rect it should choose the 16x16 image rep automatically as this is the best fit.
     
  7. McBgnr thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #7
    Thanks a bunch. It is now working with the setSize method of NSImage.
     

Share This Page