• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
What's the fastest way to get icons for file/folders?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What's the fastest way to get icons for file/folders?


  • Subject: What's the fastest way to get icons for file/folders?
  • From: "Alan Smith" <email@hidden>
  • Date: Fri, 18 Aug 2006 08:11:50 -0400

Hi everyone,
I ran my code through Shark and found that NSWorkspace's iconForFile
is too slow for me to keep in my code. So I looked around and found
some stuff on GetIconRefFromFile(). After writting some code I had a
nice NSImage category that got the icon at the path I specified. But
there are some paths (like: /Projects/App Controller, which is the
path to a directory) that it can't release the IconRef to. So, I've
pretty much given up on that and was wondering what my alternatives
were.

Thanks in advance, Alan

P.S. Below is the NSImage category.

- (NSImage*)initWithIconAtPath:(NSString*)path
{
   if ([[NSFileManager defaultManager] fileExistsAtPath: path])
   {
       FSRef fsref;
       FSSpec spec;

       OSStatus status;
       OSErr err;

       status = FSPathMakeRef([path fileSystemRepresentation], &fsref, NULL);
       if (status == noErr)
           status = FSGetCatalogInfo(&fsref, kFSCatInfoNone, NULL,
NULL, &spec, NULL);

       IconRef ref;
       err = GetIconRefFromFile (&spec, &ref, (SInt16*) NULL);

       if (err == noErr)
       {
           IconFamilyHandle fam;
           NSData *imageData;

           err = IconRefToIconFamily(ref, kSelectorAllAvailableData, &fam);
           NSAssert(err == noErr, @"can't get icon family from icon ref");

           err = ReleaseIconRef(ref);
           NSAssert1(err == noErr, @"can't release icon ref %@", path);

           imageData = [NSData dataWithBytes: *fam length:
fam[0]->resourceSize];
           NSAssert(imageData != nil, @"can't make NSData from icon data");

           return [self initWithData: imageData];
       }
   }
   return nil;
}
--
// Quotes from yours truly -------------------------
"You don't forget, you just don't remember."
"Maturity resides in the mind."
"Silence is the Universe's greatest gift."
"Don't waste your life doing things others have already done."
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: What's the fastest way to get icons for file/folders?
      • From: Corbin Dunn <email@hidden>
  • Prev by Date: Re: [self init] vs. [super init] in initWithCoder
  • Next by Date: Re: copy versus arrayWithArray:
  • Previous by thread: Re: Custom NSButtonCell and NSTableColumn binding
  • Next by thread: Re: What's the fastest way to get icons for file/folders?
  • Index(es):
    • Date
    • Thread