Reputation: 3956
I load all images in UIScrollView at one time,I know it is bad way,so is there any better way to optimize it?
Upvotes: 3
Views: 3475
Reputation: 1045
I know it is old thread, but anyone looking for solution take a look at this: https://github.com/sumofighter666/ReusableScrollView
Upvotes: 0
Reputation: 4480
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.
Upvotes: 0
Reputation: 3956
- (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];
}
}
}
Upvotes: 3