简体   繁体   中英

Optimizing UIScrollView for subview usage

I have a UIScrollView where I load a huge number of UIImage's horizontally. Which would be a better option? Loading the image only on the visible area of the UIScrollView and unload the other images which is not visible? OR Load the images on the visible area of the UIScrollview and not unload the images which was initially in the visible region and now hidden?

You should use UITableview or UICollectionView instead of UIScrollView if you want to leverage the built-in benefits of re-usability. Refer this tutorial for horizontal table-view. Add image on each cell. It will increase performance and reduce memory.

Probably the best optimization would be reusing image views. One solution is to use UICollectionView instead of UIScrollView , the other would be to manually reuse UIScrollView subviews. Check out this library it reuses scrollView subviews and has API similar to UITableView .

My recommendation in this day and age would be to use UICollectionView and allow it to handle the dynamic cell loading for you. It has a very flexible cell mechanism similar to that found in UITableView.

iOS unloads UIImages when memory gets low, and reloads them automatically as needed. However, to avoid getting to a low-mem situtation in the first place, I would recommend releasing any images you don't need, provided the performance hit due to reloading them is acceptable. Note that the UIImage method imageNamed: caches images, so you might want to use one of the other init methods.

(That is, if you really want to use a UIScrollView. As pointed out here, there are better solutions.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM