I have a UI Table View Controller. Each Cell Loads an image from my webserver.
If I scroll the TableView so that a particular cell scrolls out of view and then scroll back again the image for that cell has vanished and I have to wait for it to reload.
I'm guessing this is some performance/ memory management thing build into iphone?
How can I stop this behaviour?
I believe the cells in the tableview are recycled.
cache the images in memory and assign from your cache rather than loading the images directly into the tableview
I don't know if this is best practice or not but I think you could use an NSArray or NSDictionary of UIImage and load into there first and just assign references to the objects in the array.
Update
There is some code here which uses an NSMutableDictionary for the cache
Related
I have a UITableView. each row is heavy object with videos, images etc.
When user scrolls this view how can I release the memory of not visible rows and load the current visible rows?
I assume you're talking about releasing memory that's used by the images and videos of your row, and not the row itself.
In your tableview delegate,
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
tells you when the tableview scrolling has stopped.
[myTableView indexPathsForVisibleRows]
gives you an array of what is visible.
If your row is not in this array, it is not visible, and you can do your image/video cleanup on it.
Are you recycling UITableViewCells as per Apple's recommendations? If not, you MUST read Apple's docs. Inside the cellForRowAtIndexPath: delegate you should have something [customCell setMediaObjects:]. Inside your customCell class you can release all the previous mediaObjects from memory.
As others have said, you should make sure you are recycling cells properly, and not destroying things you would need to recreate anyway when the cell is reused.
But, you may want to release other assets that the cell or its views are retaining. Or if that cell has any pending download requests, for example, you may want to reset their priority or even cancel them when the cell is offscreen.
I think the cleanest way to do this is to just override -[UITableViewCell prepareForReuse]
This is called when the cell is put back into the reuse queue. If the user is moving up and down the table quickly, you may not want to clean the cell up the moment the cell is off the screen (by looking at indexPathsForVisibleRows, for example).
But when the cell is actually put back in the reuse queue, that is a good time to do that work since you know that cell won't appear again on screen until you dequeue and configure it again.
A Closer Look at Table-View Cells - Apple Documentation
When you call dequeueReusableCellWithIdentifier first 10 (just ten cells can be shown at screen at one moment) cells will be created, then they will be just loaded and configured for showing.
So, cells are not released.
I've a UITableView and each UITableViewCell displays a unique UIImage which is fetched from internet. As we scroll the UITableView, cells are recreated and tableView: cellForRowAtIndexPath: gets called every time to configure cells. So it loads UIImages again and again from internet and scrolling is not smooth.
The solution I found for myself right now is to create NSMutableArray of all those UIImages on ViewDidLoad then load images into UITableViewCells from that NSMutableArray which is for sure giving me smooth scrolling.
My concern with my own solution is that when I keep all UIImages in NSMutableArray and those all objects are kept in memory for as long as application runs, I am most probably making inefficient use of memory.
Is there a better, more efficient way to do this which gives me smooth scrolling as well as best memory usage?
I think this example will help you..
http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/
Cache the downloaded images on the filesystem. When loading a cell, first ensure that a cached copy of the associated image is available, then load it using [UIImage imageWithContentsOfFile:].
You should go with array of image link instead of the image.
You should load image asynchronously so that it will improve performance of creation of the UITableviewCell.
Verify that you are using dequeueReusableCellWithIdentifier, it will give you the smoother scrolling.
HI,
I am getting my images using JSON and I need to display image at a time when my app is running so please tell me how to use asynchronous method to load the images into the UIScrollView?
Thanks.
whether you use a UITableView or a UIScrollView, you will use the same pattern, its just that you have a bit more plumbing to do with the UIScrollView. I would say use the UITableView unless you want to roll your own multi-column image thumbnail picker.
With the UIScrollView, you will need to create your own virtual rows and columns and detect when the ScrollView has scrolled a new row or a new column into view. You will need to queue (remove) the images that have scrolled out of view, and then load the images that have come into view. All in all, this is not trivial code.
The UITableView does all this for you (rows only, no concept of columns)
Having said that.. here is a reference to using the NSOperation class to lazy load your images into a UITableView while giving the user a smooth experience..
UIImage in uitableViewcell slowdowns scrolling table
i have created scrollview and have loaded tableview on that programmatically .also i have created customize tableview cell and displaying data in tableview.But my problem is whenever i am scrolling tableview vertically my data is not getting persist and data of cell is getting erase.Please help me how shuld i store that data
It sounds as if you might not be using the reuseidentifiers of the table view properly. If you give all cells the same reuseidentifier then as you scroll the cells that go off screen might end up losing the customizations you made to them. I suggest getting a good understanding of reuseidentifiers, you could start here:iphone-what-are-reuseidentifiers-uitableviewcell
I'm trying to get my UITableView to show cells with images placed on them (contained in a UIImageView overlaid). I'm wondering why when scrolling up and down, the images look like they're overlaid on top of one another.
What can I do in this case for the sake of memory management as well as to fix this issue?
Make sure that when you're dequeuing a reusable cell that you remove whatever image view was in the cell before you add the one for the current index path.
Alternatively, you can change the image property of the UIImageView.