A lot of images in tableView - ios

I have tableView with collectionView inside.
I have ~500+ images in collectionView.
Firstly I use paths and load images in collectionView from paths from collectionView:cellForItemAtIndexPath: . But it was slow and I begin to fill collectionView with UIImages from array. It became faster but it must be more faster. How can I accelerate it?

Take a look at FastImageCache, a fast loading image cache. The requirements/assumptions don't fit every use case, but from your problem statement it could work for you.

Related

UICollectionview Scrolling while adding Items is super laggy

I have this very nice collectionview that loads pictures from my server. Now, if the pictures are loading slowly, the collectionview is actually scrollable, but if the pictures are loading fast, instead, the collectionview is basically frozen until all the cells are finally loaded.
I'm pretty sure this is something related to the fact that I'm using dispatch_async to update the ui (inserting cells and sections), so I'm wondering if there's a way to update the ui that still allows me to scroll the collectionview while the items are being added.
Is there anything I'm missing?
Thanks in advance for your help!
The easiest way to solve this issue is to use SDWebImage an UIImageView alternative , and call the sd_setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.
You can find it here: https://github.com/rs/SDWebImage
So remove any dispatch_async, set right away the number of cells on this method:
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
Scroll performance is going to be good with this tool

Swift stop unloading of table view cells?

I'm making a table view that downloads an image for the cell off the web, every time the cell is loaded.
This proves a problem though, as when you scroll down then up again, the cells are reloaded.
Is there any way to stop the unloading of cells, and control how many cells are loaded when the view appears?
I propose that instead of trying to stop your UITableView from reusing cells, which would decrease performance, you store the images you download, using NSCache for example.
NSCache works the same way as a Dictionary - with key value pairs. The main difference however is it incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. Here's the documentation on NSCache: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSCache_Class/index.html
Using this method: when you're setting up your UITableViewCell, you could check if an image has been downloaded for that NSIndexPath. If an image hasn't been downloaded you download it and add it to the cache once its finished, otherwise you use your cache and get the image from there.

How to add images one by one as I scroll

I have an requirement where I had to load multiple images and videos in the ScrollView
but on iPad 2 my application crashes when I load entire set on one go.It says that application
exits due to heavy memory pressure.
I would like to know how to handle this issue and it it possible to load images one by one when i scroll? and if yes then how it can be done
I think The Best will be to use UITableView instead of scrollView. As tableview uses DEQUEUE so it will do the part of loading/unloading the visible portion of tableview. Table view cell can be customized any way You want.
Hope it will help.
Paging is the way to go.
If you load all the images in the memory then you are compromising scalability in big way for one or two image it will be fine for large number say hundreds it will crash.
One approach is you keep only three images in memory previous image----current image being displayed and ------next image ready for display. And you have to use lazy loading and image caching
Following links will help you
.Icarousel
.AFnetworking's uiimageview category for lazy loading and caching
.ASImageView a light weight uiimageview category if you dont want to integrate full AFNetworking in your project

UITableViewCell image from web

I'm going to download images and titles from web (probably using AFNetworking) and insert inside UITableViewCell. Since images are not same size I'll need to calculate size for every cell.
Currently in my Post model I have title and imageURL. Should I first download all images, insert in array and then add to cell or should I use that AFNetworking function inside cellForRowAtIndexPath and download images from there?
Since I'm going to need images in both cellForRowAtIndexPath and heightForRow (for calculation), downloading all images first and storing in array might be better solution but dunno if cache is doing all the work so I could go with second approach?
Thanks.
Fot the better performance,
You should approach for the Small sizes of images or few.
downloading all images first and storing in array.
IF the images are huge, then should go for the Asynchronous task to display.
and then Use the custom UIImageView within the cell to show the image.

Tricks for improving iPhone UITableView scrolling performance?

I have a uitableview that loads fairly large images in each cell and the cell heights vary depending on the size of the image. Scrolling performance is decent, but can sometimes be jerky.
I found these tips I found on the FieryRobot blog:
glassy-scrolling-with-uitableview
more-glassy-scrolling-with-uitableview
Does anyone have any tips for improving uitableview scrolling performance?
Cache the height of the rows (the table view can request this frequently)
Create a least-recently-used cache for the images used in the table (and invalidate all the inactive entries when you receive a memory warning)
Draw everything in the UITableViewCell's drawRect: if possible avoid subviews at all costs (or if you require the standard accessibility functionality, the content view's drawRect:)
Make your UITableViewCell's layer opaque (same goes for the content view if you have one)
Use the reusableCellIdentifier functionality as recommended by the UITableView examples/documentation
Avoid gradients/complicated graphical effects that aren't pre-baked into UIImages
If you are subclassing
UITableViewCell, don't use a Nib,
write it in code instead. It's much
faster than loading Nib files.
If you're using images, make sure
you're caching them so you don't
have to load from file more than
once for each (if you have the
memory -- you'd be surprised how
much space images take up).
Make as many elements opaque as
possible. Similarly, try not and use
images with transparency.
The developer behind Tweetie has written extensively about this and has some code that demonstrates how it was done for that app. Basically, he/she advocates one custom view per table cell, and drawing it manually (rather than subviewing with Interface Builder, among other options).
fast-scrolling-in-tweetie-with-uitableview
Also, Apple has updated its own sample code for TableView in its TableViewSuite tutorials (maybe in response to this?)
TableViewSuite
#1 performance killer for UITableView scrolling is drawing shadows on any cell view layer, so if scrolling performance matters then don't do shadows unless basically it doesn't slow down your main thread.
thought this had to be said since none of the accepted answers made mention of shadows and layers. :+)
Any problem with UITableView scrolling performance can be solved using techniques already described in other answers. However many a times sluggish performance is caused by something inherently erroneous, or repetitive.
The fact that UITableView reuses the cells, and the fact that each cell may need its own image - together makes the solution bit complex. From how it's being solved the general way, here I summarize things that should be taken care of:
Load data into data source - from REST / database. This step should be done on background, eventually using dispatch_async along with GCD queue.
Create and initialize relevant data model objects and putting them inside an array
[tableView reloaddata]
Inside cellForRowAtIndexPath, include code that will set data (text) from correct data model object of the array.
Now images maybe in the form of URL too, so this step might be little quirky because of cell reuse done by table view. The heart of the fact is to load once again image from device cache / URL using async queue, then set it to correct cell.image (whatever is your cell image property).
To avoid problems, refer to this tutorial about lazy loading of images inside table view.

Resources