UIScrollView 延迟加载图片以减少内存使用并避免崩溃

UIScrollView lazy loading of images to reduce memory usage and avoid crash(UIScrollView 延迟加载图片以减少内存使用并避免崩溃)
本文介绍了UIScrollView 延迟加载图片以减少内存使用并避免崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的应用程序,使用滚动视图,通过 NSOperation 加载多个图像(最大约 100sh).我试图在我的 ipod 2Gen 上对其进行测试,但由于设备内存不足而崩溃,但在 ipod 4th Gen 上运行良好.在第 2 代,它在加载大约 15-20 个图像时崩溃.我应该如何处理这个问题?

My app, using scrollview that loads multiple images with NSOperation (Max around 100sh). I tried to test it out on my ipod 2Gen and it crashes due to low memory on device, but works fine on ipod 4th Gen. On 2nd Gen, it crashes when it loads about 15-20 images. How should I handle this problem ?

推荐答案

你可以懒惰地加载图片.这意味着,例如,在您的滚动视图中一次只有几张图像,以便您可以动画到下一张和上一张;当您向右移动时,例如,您还加载了一张图片;同时,您会卸载不再可直接访问的图像(例如留在左侧的图像).

You could load you images lazily. That means, e.g., just a couple of images at a time in your scroll view, so that you can animate to the next and the previous one; when you move to the right, e.g., you also load one more image; at the same time, you unload images that are not directly accessible anymore (e.g. those that have remained to the left).

您应该使预加载图像的数量足够多,以便用户可以随时滚动而无需等待;这还取决于这些图像有多大以及它们来自哪里(即加载它们需要多长时间)......一个很好的起点是,IMO,随时加载 5 张图像.

You should make the number of preloaded image sufficiently high so that the user can scroll without waiting at any time; this also depends on how big those images are and where they come from (i.e., how long it takes to load them)... a good starting point would be, IMO, 5 images loaded at any time.

在这里您会找到不错的分步教程.

由于上面的链接似乎已损坏,以下是该帖子的最终代码:

Since the link above seems to be broken, here is the final code from that post:

-(void)scrollViewDidScroll:(UIScrollView *)myScrollView {

/**
 *  calculate the current page that is shown
 *  you can also use myScrollview.frame.size.height if your image is the exact size of your scrollview
 */
int currentPage = (myScrollView.contentOffset.y / currentImageSize.height);

// display the image and maybe +/-1 for a smoother scrolling
// but be sure to check if the image already exists, you can do this very easily using tags
if ( [myScrollView viewWithTag:(currentPage +1)] ) {
    return;
}
else {
    // view is missing, create it and set its tag to currentPage+1
}

/**
 *  using your paging numbers as tag, you can also clean the UIScrollView
 *  from no longer needed views to get your memory back
 *  remove all image views except -1 and +1 of the currently drawn page
 */
for ( int i = 0; i < currentPages; i++ ) {
    if ( (i < (currentPage-1) || i > (currentPage+1)) && [myScrollView viewWithTag:(i+1)] ) {
        [[myScrollView viewWithTag:(i+1)] removeFromSuperview];
    }
}
}

这篇关于UIScrollView 延迟加载图片以减少内存使用并避免崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Cancel current UIScrollView touch(取消当前 UIScrollView 触摸)
Can UITableView scroll with UICollectionView inside it?(UITableView 可以在里面滚动 UICollectionView 吗?)
How to make a screenshot of all the content of a Scrollview?(如何截取 Scrollview 的所有内容?)
Swift iOS Set scrollView constraint below navigation bar programmatically(Swift iOS以编程方式在导航栏下方设置滚动视图约束)
UILabel sizeToFit method not working properly(UILabel sizeToFit 方法无法正常工作)
Get the current UIScrollView scroll values on the iPhone?(获取 iPhone 上当前的 UIScrollView 滚动值?)