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: 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