• 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
Re: Using NSImages as keys to a dictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using NSImages as keys to a dictionary


  • Subject: Re: Using NSImages as keys to a dictionary
  • From: Greg Hurrell <email@hidden>
  • Date: Sat, 13 May 2006 10:29:28 +0200

El 13/05/2006, a las 8:24, Lawrence Sanbourne escribió:

My app needs a way of caching scaled images (thumbnails) so that they
aren't recreated each time. I first did this using an
NSMutableDictionary mapping from original image to scaled image.
However, this didn't work because apparently one original image wasn't
isEqual: to another.

I realized that I just wanted pointer equality. So I did this:

// originalIcon is an NSImage *
[cachedIcons objectForKey:[NSNumber numberWithInt:(int)originalIcon]];

It works beautifully. However, this code seems really weird to me. Is
there a better way?

According to the docs objects added to dictionaries are retained but keys are copied. So when you use your thumbnail as a key it is actually getting copied, thus changing the pointer. As you've discovered "isEqual:" evidently isn't doing a low-level comparison to see if it's really the same image and the equality test was failing.


Although the test you describe looks "weird" to you it is in fact the way you would do it if what you actually want to compare on is pointer equality. The down-side of this approach is that you then have to worry about the lifespan of the thumbnail objects seeing as the dictionary is neither copying nor retaining them.

As already suggested, an NSImage subclass might be more elegant. The other alternative is a compound class (with instance variables for the full-size image and for the thumbnail), or just use dictionaries directly with keys like "image" and "thumbnail". If you do this in a key-value coding compliant way then you can use it with bindings.

Cheers,
Greg

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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: Using NSImages as keys to a dictionary
      • From: "Adam R. Maxwell" <email@hidden>
References: 
 >Using NSImages as keys to a dictionary (From: "Lawrence Sanbourne" <email@hidden>)

  • Prev by Date: Re: MediaCenter/FrontRow
  • Next by Date: Re: Subviews becoming blurry or "chopped up"
  • Previous by thread: Re: Using NSImages as keys to a dictionary
  • Next by thread: Re: Using NSImages as keys to a dictionary
  • Index(es):
    • Date
    • Thread