0

我只是想知道是否有人可以为我解释这段代码,以便我可以从中学习。我试图让我的应用程序有一个滚动器,它可以从左到右滚动大量图片(来自互联网),但问题是,它必须具有延迟加载。所以我做了一些教程并想出了如何去做,但我真的不明白。所以我希望某个善良的灵魂会解释如何一步一步地延迟加载

这是我从教程中学到的代码:

-(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];
    }
}

}

4

3 回答 3

1

关于滚动视图的延迟加载,我强烈建议改用 UITableView。苹果在这个组件的性能方面做得很好。

如果您想要连续滚动(pagingEnabled = NO;),您可以将它们水平放置(请参阅此EasyTableView 代码,效果很好)并停止页面模式,这样您就可以获得您正在寻找的行为。

于 2012-08-01T08:31:50.703 回答
0

Lazy loading is basically fetching large pieces of data (lets say images in this example) only when you need them. In your code, you have a delegate method that is called when you scroll a UIScrollView.

The -(void)scrollViewDidScroll:(UIScrollView *)myScrollView function decides when to actually get data. So as your scrolling, you find out where you are in the scroll view (say you have 10 images you want to load-- you want to know if the screen is currently showing image number 1, 2, 3, etc.). This is what the currentPage integer holds.

Now that you know which page you're looking at, we want to actually fetch the image.

if ( [myScrollView viewWithTag:(currentPage +1)] ) {
    return;
}

The code above checks if the image AFTER the image the person is currently looking at exists (hence the currentPage + 1). If it does, we've already fetched it and we quit the function. Otherwise:

else {
    // view is missing, create it and set its tag to currentPage+1
}

Here, we lazy load the image. This is done, for example, by creating a new thread and downloading the image from a server. We do this while the view is not the currentPage because we don't want the image to "pop in" while the user is scrolling. The view to which we add the image gets a tag (UIView has a "tag" property); we set the tag to currentPage+1, which later allows us to index the view in case we need it.

Finally, we have:

/**
*  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];
    }
}

Here, we use our currentPage variable and iterate through all our views by indexing them by the tag we set. If the tag is not one off from the currentPage (remember, we don't want any pop in!) we remove it from the scrollview and free some memory up.

Hope that helped.

于 2012-08-01T04:39:36.653 回答
0

也许这会对你有所帮助。

  1. 从这里下载异步 ImageView 文件https://github.com/nicklockwood/AsyncImageView/并将它们包含到您的项目中。

  2. 将 ImageView 拖到 xib 文件上并将其类更改为 AsynchronousImageView 而不是 UIImageView

  3. 把它写在你的 .h 文件中

    IBOutlet AsynchronousImageView *_artworkImg;

  4. 把它写在你的 .m 文件中

    [_artworkImg loadImageFromURLString:@"你的图片网址"];

于 2012-08-01T08:18:45.603 回答