Reputation: 2225
I'm building an app that allows users to view pictures off a website. I load the pictures on screen, and then once the user taps on one, I give him the option to add it to his favorites.
So as to allow him to view the images offline, I store the UIImageJpegRepresentation in an NSMutableDictionary which later goes into NSUserDefaults.
When the user is in the online-image viewing section, if he comes across a favorited image, he can remove it from favorites (I check if that's one of his favorites by comparing the NSData to what I'd stored into NSUserDefaults).
Now here's the problem: when the user goes into the favorites section and loads the images from NSUserDefaults, the comparison of the NSData in NSUserDefaults to the NSData of the image in the image views on the screen fails. So when he taps on an image while in the favorites section, it's not detected that the image itself is a favorite in NSUserDefaults and I assume the two NSData are different for some reason.
Could someone please shed a light on why this happens and potentially give a hint on how to solve it?
Upvotes: 0
Views: 230
Reputation: 11330
The images are potentially being compressed differently by UIImageJPEGRepresentation
-- JPEG is a lossy format.
Other than that, this does not seem like the best way to achieve your stated goal. If the user is viewing a lot of images and has a lot of favorited images, this will perform very poorly. A much better solution is to store some sort of image ID, e.g. a URL. Comparing URL strings is likely to be orders of magnitude faster than comparing whole images.
Upvotes: 1