I'd create a FavoriteImage object. Give it one property, a UIImage named image. Also, make it comply to the NSCoding protocol. Here is a tutorial on how to make it comply to NSCoding:
How To Save Your App Data With NSCoding and NSFileManager | Ray Wenderlich You can ignore the stuff about NSFileManager.
Then, I'd create a singleton (Duncan C shows how to make a singleton here:
http://www.iphonedevsdk.com/forum/ip...singleton.html) and give it 3 methods and a NSMutableArray property named internal_favoriteImages:
Code:
- (void)addFavoriteImage:(FavoriteImage *)favoriteImage;
- (void)removeFavoriteImage:(FavoriteImage *)favoriteImage;
- (NSArray *)favoriteImages;
In addFavoriteImage, add the passed in image to your internal_favoriteImages array. Then, wrap your array into a NSData object using NSKeyedArchiver's archivedDataWithRootObject: class method. For this to work, your FavoriteImage object needs to comply to NSCoding. Then save the NSData object to NSUserDefaults.
In removeFavoriteImage, do the same thing as you did in addFavoriteImage except remove the passed in image from the array.
In favoriteImages, create a non mutable version of the array using NSArray's arrayWithArray: method and return that.
In your Singleton init method, check if a NSData object has been saved into NSUserDefaults and if so, retrieve the NSData object. Unpack it using NSKeyedUnarchiver's unarchiveObjectWithData: class method. If there is no NSData object present, just initialize your internal_FavoriteImages array.
The only problem with this approach is you are going to be saving the image data again which causes loss of disk space. I'm too lazy at the moment to figure out a solution to that so I'll let someone else tackle that problem.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com.
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.