Re: Using NSImages as keys to a dictionary
Re: Using NSImages as keys to a dictionary
- Subject: Re: Using NSImages as keys to a dictionary
- From: "Adam R. Maxwell" <email@hidden>
- Date: Sat, 13 May 2006 09:19:32 -0700
On May 13, 2006, at 01:29, Greg Hurrell wrote:
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.
Another way to do this might be to create a CFMutableDictionary with
custom callbacks that retain keys instead of copying them, and
compares keys based on pointer equality; with toll-free bridging,
you'd just treat it as a standard NSMutableDictionary. You have to
ensure that the hash of the keys (NSImage instances) doesn't change
while they're in the dictionary, though.
--
Adam
_______________________________________________
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