How to optimize UIScrollView with large numbers of images

混江龙づ霸主 提交于 2019-12-12 15:41:42

问题


I load all images in UIScrollView at one time,I know it is bad way,so is there any better way to optimize it?


回答1:


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int currentPage = (scrollView.contentOffset.x / scrollView.frame.size.width);

    // 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 ([scrollView viewWithTag:(currentPage + 1)]) {
        return;
    } else {
        // view is missing, create it and set its tag to currentPage+1
        UIImageView *iv = [[UIImageView alloc] initWithFrame:
            CGRectMake((currentPage + 1) * scrollView.frame.size.width,
                       0,
                       scrollView.frame.size.width,
                       scrollView.frame.size.height)];
        iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",
                                                                  currentPage + 1]];
        iv.tag = currentPage + 1;
        [sv addSubview:iv];
    }

    /**
     * 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 < 50; i++) {
        if ((i < (currentPage - 1) || i > (currentPage + 1)) &&
            [scrollView viewWithTag:(i + 1)]) {
            [[scrollView viewWithTag:(i + 1)] removeFromSuperview];
        }
    }
}



回答2:


You can use this tutorial to help you out. Although I also recommend what user1212112 said, and watch WWDC 2011 Session 104 - Advanced Scroll View Techniques.




回答3:


VSScrollview have a look at VSScrollview. It reuses its views you pass to it. Its use is simple as using UITableview. Implement following datasoure methods

-(VSScrollViewCell *)vsscrollView:(VSScrollView *)scrollView viewAtPosition:(int)position;
// implement this to tell VSScrollview the view at position "position" . This view is VSScrollviewCell or subclass of VSScrollviewCell.

-(NSUInteger)numberOfViewInvsscrollview:(VSScrollView *)scrollview;

// implement this to tell VSScrollview about number of views you want in VSSCrollview.

There are other optional datasource and delegate methods too by which you can customize the behavior of VSScrollview You can have different width, height, spacing and even content size of scrollview for each view.




回答4:


I know it is old thread, but anyone looking for solution take a look at this: https://github.com/sumofighter666/ReusableScrollView



来源:https://stackoverflow.com/questions/10221591/how-to-optimize-uiscrollview-with-large-numbers-of-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!