How to speed up the tableview's performance with many images? - ios

I've developing an iPad app.And I have a uitableview, in which every cell has about four to five images.And almost four to five cells on screen.
So there are about 20~30 images on screen.
The images are not very large , perhaps 200*200.(already some kind of thunbnail)
All the images are local resource.
The problem is that when first scroll down the tableview, it needs to load new cells and of course the images in the cells. This makes the UI become laggy.
But when you scroll back up ,it's very smooth. I know this is because UIImage class will cache some images.when scroll back up, the images are cached, so it's smooth.
So my question is how to deal with this?
1.Make as small thumbnail as possible?
2.Pre-load some image?(How?)
3.Use custom cache?(also how?)

I would recommend fetching the images with GCD. I would also subclass the UITableViewCell and in it's drawRect: draw all pictures on the view's layer. That will increase your performance dramatically.

If there is not so much images in the table - you can precache by loading them, when the view is created. Otherwise use more generic approach with async loading in background thread.

Related

should I resize images for displaying in a UITableView?

I am using a tableView that among other things display images in a UITableViewController. The images are locally stored and quite large so that they may if clicked be displayed full screen. The tableView cell has a small preview of these images.
When storing these images locally, is it better to store two versions? A thumbnail and a full image purely for the performance of the tableView? Or will that not effect the scrolling etc to have just the one full image and allow the tableView cell to resize the image to display at the reduced size?
I will not be uploading/downloading so this question is not about creating thumbnails to optimize download transfers.
I am just interested in whether it is best practice to create a thumbnail for tableView controller cell's.
Also, I am familiar with setting aspectFit for the imageView to get the correct size. Just wondering if I should be carrying two images for better performance or if it doesn't make any difference and not necessary to do that.
I am just interested in whether it is best practice to create a thumbnail for tableView controller cells
Yes! It is best practice to create a thumbnail for any image display, especially table view cells. To hand an image view an image that is too large is a massive waste of memory and other resources.
See Image and Graphics Best Practices from WWDC 2018 for details.

Loading multiple cells from XIBs in UICollectionView makes scrolling laggy on the first load

I have a collection view with like 7-8 different cells. Cells are constructed in XIB files. Then in View Controller I use:
[self.collectionView registerNib:[UINib nibWithNibName:name bundle:nil] forCellWithReuseIdentifier:name];
Everything works as expected, but there are some performance issues. On the first scroll collection view noticeably lags as cells appear. After all kind of cells have been loaded at least once everything starts to scroll smoothly. Cells are not really complicated.
Q: Why do you even use XIBs for that, why not prototype cells in a storyboard?
A: Same cells are used in different collection views throughout the app. This way I can apply changes to a cell once in a xib. I couldn't come up with easier solution to do that.
I'm pretty sure UINib is cached after it's loaded and this is why it stops lagging. I was wondering if there's a way to preload those xibs at the splash screen for example. It will take a second or two, but will result in smoother experience.
UINib are cached once loaded, there are plenty of reason that could make scrolling laggy:
Are you doing some complex layout calculation?
Are you setting in cells huge images or doing intensive task on the main thread to resize them?
Do you have complex transparencies ?
Are you doing something that requires off-screen rendering, for instance cornerRadius on a layer ?
Using instruments you can really check most of those issues, for instance Time Profiler will help you in checking if something is blocking the UI, using simulator you can activate Color Blended Layer to search for transparencies (in red) and Color Offscreens render to check for offscreen render views (in yellow)

iOS - UIImageView slight glitch

I have a myriad of UIImageViews that act as 'tiles' on a map. They align up next to one another to form a grid of images.
Ordinarily, the grid images are flush against each other, but whenever the iPhone or iPad device is rotated, or if a UIView is applied to the view that contains these tiles, the spaces between the UIImageViews become momentarily visible, showing off the grid pattern that I'd like to be invisible.
Any ideas what might be causing this?
Thanks
Consider using CATiledLayer to draw all the images in a single view instead of using multiple subviews. There's a nice writeup on using it at Cocoa Is My Girlfriend. CATiledLayer makes it easy to build an image out of smaller tiles, display higher resolution images as the user zooms in, and avoid memory problems that come from keeping too much of a large image loaded.
this is happening because you are using different views (here uiimageview). Since they are not part of the same fabric during any animation or change in view hierarchy there is a chance that the gap between them would be visible.
This is a very common problem in graphics programming. The way to fix this problem is to have one single view and add these uiimageviews as sub-layers to this view. That way all the imageviews are part of the same fabric and the gap would not be visible.
I did not post any code but then so did you ;)

Better to use a small image that resizes or true sized image for UITableViewCell background?

I'm trying to address a slow scrolling and slow loading UIViewController that uses a UITableView with custom background cells.
When I don't set the backgroundView of the cell with an image, the performance is great. So I'm wondering if it's more performant to supply the backgroundView with images that are the actual size of my tableview row or is it better to use smaller images that are resized to fit the row's dimensions?
The images are all local and already included in the app - there's no remote fetching involved.
Improving load time of UITableView that has cells with custom background images
Try to look to use an NSOperationQueue to handle lazy loading of images and a custom tableviewcell.
Google for tweetie custom tableviewcell That should set you in the right direction.
Apple has a sample project for downloading images in tableViews: LazyTableImages

Transparent custom UITableViewCell

I've finally finished my first large application, the only problem is that I've focued a lot on design, and I'm using custom nibs as cells with transparent backgrounds. When I tried testing the application on my iPhone, the performance was terrible.
Is there any way to get better scrolling performance while using transparent cells with a ImageView behind the UITableView?
I've read two articles mostly:
blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
Looks good, but what if I want to use transparent cells?
a) Uses solid color.
b) Uses imageview as background.
Any help will be greatly appreciated. I want to get this baby released as soon as possible, but the performance as it is now is terrible!
First off, stop using nibs. Every time a cell is created, you're now hitting the disk in order to unarchive the nib. 3.1 will actually make this better, but until then, please create your cell in code.
Secondly, remove transparency wherever you can. Anything that doesn't need to be transparent, shouldn't be. And anything that isn't transparent should have the opaque property set to YES.
A third suggestion is if you're using a lot of subviews, you will see a performance benefit by using a custom view to draw everything instead of a bunch of subviews. If you choose to go this route, you should consider how it behaves when rotating to landscape mode (e.g. how the stretch action occurs), or if you have any controls that need to handle touches separately from the cell itself.

Resources