hey all. i have to download about 2000 images in a scroll view. i get them from URL. in my first attempt i added all of them as subview to scroll view but after a certain time the application is crashed. then i searched over web and some people faced some problem also. in a website i found such a solution.
the problem in here is lets say i am in 5th page just 4th and 6th pages are added to scrollview but when the user scrolls to 3rd page to see it then it cant be seen and not loaded back. what can be wrong here ?
Code:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int currentPage = (scrollView.contentOffset.y / scrollView.frame.size.height);
NSLog(@"current page : %d", currentPage);
// 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(0, (currentPage + 1) * scrollView.frame.size.height, scrollView.frame.size.width, scrollView.frame.size.height)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *imgResim = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.arrDeneme objectAtIndex:currentPage]]]];
dispatch_async(dispatch_get_main_queue(), ^{
iv.image = imgResim;
iv.tag = currentPage + 1;
[self.scrResimler 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];
}
}
}
the problem in here is lets say i am in 5th page just 4th and 6th pages are added to scrollview but when the user scrolls to 3rd page to see it then it cant be seen and not loaded back. what can be wrong here ?