如何使用大量图像优化的UIScrollView(How to optimize UIScrollVi

2019-09-16 15:17发布

我加载UIScrollView中的所有图像在同一时间,我知道这是不好的方式,那么,有没有更好的方法来优化它?

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


Answer 2:

您可以使用此教程,以帮助你。 虽然我也推荐一下user1212112说,看WWDC 2011 Session 104 - Advanced Scroll View Techniques



Answer 3:

VSScrollview看看VSScrollview。 它重用你传递给它的意见。 它的使用也很简单使用的UITableView。 执行以下datasoure方法

-(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.

还有其他可选的数据源和委托方法也通过它可以定制VSScrollview的行为,你可以有不同的宽度,高度,间距,甚至滚动视图为每个视图内容的大小。



Answer 4:

我知道这是旧线,但任何人都在寻找解决方案来看看这个: https://github.com/sumofighter666/ReusableScrollView



文章来源: How to optimize UIScrollView with large numbers of images