Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4234272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:19:52+00:00 2026-05-21T02:19:52+00:00

How do you write out a 16×16 PNG file for a file type icon

  • 0

How do you write out a 16×16 PNG file for a file type icon in Cocoa? I tried this before with code like the snippet below. The snippet worked on a PPC machine with Mac OS X 10.5, but no longer seems to work on Mac OS X 10.6: the PNG that’s written out has size 512×512, rather than 16×16.

NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kGenericFolderIcon)];
[icon setSize: NSMakeSize(16.0,16.0)];
NSBitmapImageRep * bitmapRep = [NSBitmapImageRep imageRepWithData: [icon TIFFRepresentation]];  
NSData * data = [bitmapRep representationUsingType: NSPNGFileType properties: nil];
[data writeToFile: @"/tmp/test.png" atomically: NO];

From what I understand from the documentation on NSImage and NSImageRep, the fact that the above code worked before was coincidence, as setSize: only sets the “drawing size” of the image which doesn’t necessarily match the “physical size” of its representation(s).

So what is the correct way to get the 16×16 size PNG file? Also, icons on Mac OS X can include a specially-drawn version for the 16×16 size, which is not just a scaled-down version of the “big” icon; how do you make sure this special version is written out to the PNG file when one is available?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:19:53+00:00Added an answer on May 21, 2026 at 2:19 am

    If you are sure that there’s a 16×16 sized version of the icon present in the NSImage, you could look through the representations (-[NSImage representations]) and select the one you’re interested in by looking at the size (-[NSImageRep pixelsWide] and pixelsHigh). You could then write the representation to disk.

    A better way would be to create a 16×16 bitmap context, draw the image into that context and then save the contexts contents. This way also works if the original icon does not contain a 16×16 representation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write out a text file. Instead of the default UTF-8, I
I'm trying to write out a Byte[] array representing a complete file to a
I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
Im trying to write out an image file that has the following structure: P5
I have an array of shorts (short[]) that I need to write out to
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
I'm trying to write a regex that will parse out the directory and filename
I'm just starting out writing trying to write a simple program in C and
I am trying to figure out how to write a new line of text
I cannot figure out how to make a C# Windows Form application write to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.