如何优化UIScrollView与大量的图像

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

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

你可以使用这个教程来帮助你。 虽然我也推荐user1212112说什么,并观看WWDC 2011 Session 104 - Advanced Scroll View Techniques

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的行为。对于每个视图,可以有不同的宽度,高度,间距甚至滚动视图的内容大小。