views:

532

answers:

1

I have two very simple, identical UITableViews in my app that are populated with thumbnail images named "thumb1.jpg", "thumb2.jpg", etc. These thumbnails have associated original images "1.jpg" and text files "1.txt" used for image processing. Everything is stored in the app's Documents folder.

I want to keep the numbered, ordered naming for these files since it makes displaying the thumbnails in the UITableViews very easy with cellForRowAtIndexPath. I'm currently using a NSMutableArray (index: 1 object:"thumb1.jpg", etc) to track all images in the app.

The issue is that users can add/delete images so maintaining the order is important. For handling adding/deleting I'm looking at using insertObjectAtIndex and removeObjectAtIndex on the NSMutableArray, which will maintain order but will require programmatically changing image and text file names when this happens. For example, if there are five images in the array "0.jpg","1.jpg","2.jpg","3.jpg","4.jpg" and the user deletes the second image ("1.jpg") the array will now have "2.jpg" at index 1 so filenames will have to be changed to "1.jpg", "1.txt", and "thumb1.jpg".

How does this approach sound? I'm new to Objective-C so if you have other functions you'd use, etc I'd be interested to hear your opinion.

A: 

Renaming the actual image files themselves doesn't sound like a good idea.

I would implement 2 NSMutableArrays, one to hold the title/description of the image and one to hold the filename (or, if you wanted, the actual UIImage instead). Then if you need to delete, for example the item at index 2, deleting the same object from each of the two arrays will then leave them in sync.

If you start wanting more and more things to be stored for each row, I suggest you implement your own class. You can then implement an array of multiple instances of that class, and each class would have properties such as image, text, thumbnail etc. In fact, because you've already named three separate properties (main image, text and thumbnail) I'm tempted to say you should implement a custom class straight away.

Let me know if this makes sense or if you'd like some code to further illustrate it.

h4xxr
Each UITableView will have at least ten images so I haven't been persisting UIImages in an array because of memory concerns (the UITableViews already scroll slowly with five thumbnails each). In cellForRowAtIndexPath I create each UIImage with initWithContentsOfFile, and as I understood it the UITableView would retain this UIImage and re-insert it into its cell when requested (I'm re-using cells) without having to load from file again. Should I instead be alloc/initializing these images in viewDidLoad and storing them in a NSMutableArray?
Ian
Yes, I would say you should do this. I don't believe it's memory problems you've experienced with 5 images (I have UITableViews showing hundred of images all stored in an NSArray), but rather I believe it's the loading of images. If you are initialising each image from its file in cellForRowAtIndexPath then it is definitely reloading it each time, regardless of whether you're re-using your cells. You should then see a much faster rendering of your cells by loading the images from file in viewDidLoad - let me know if you don't.
h4xxr
This approach is working much better! I thought that a UITableView would retain any image added to it, but it looks like if you're re-using cells the image doesn't hang around after it's cell is removed. Thanks for your help h4xxr.
Ian
You're welcome Ian, and welcome to StackOverflow!
h4xxr