Reputation: 45
I have scroll view with images, but if i add many images (>10), app crash (memory limit). I use iPad 4 and don't know why it happening. Can you help fix it? My code is:
- (void)viewDidLoad
{
[super viewDidLoad];
//Put the names of our image files in our array.
imageArray = [[NSArray alloc] initWithObjects:@"salon1.jpg", @"salon2.jpg", @"salon3.jpg", @"salon4.jpg", @"salon5.jpg", @"salon6.jpg", @"salon7.jpg", @"salon8.jpg", @"salon9.jpg", @"salon10.jpg", @"salon11.jpg", @"salon12.jpg", @"salon13.jpg", @"salon14.jpg", @"salon15.jpg", @"salon16.jpg", @"salon17.jpg", @"salon18.jpg", nil];
for (int i = 0; i < [imageArray count]; i++) {
//We'll create an imageView object in every 'page' of our scrollView.
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
[self.scrollView addSubview:imageView];
}
//Set the content size of our scrollview according to the total width of our imageView objects.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count], scrollView.frame.size.height);
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
@synthesize scrollView;
@synthesize pageControl;
@synthesize imageArray;
Upvotes: 0
Views: 220
Reputation: 2607
Your app crashes because each device has limited amount of memory. The problem is that you are trying to load all images at once. Instead, try UICollectionView or UITableView - they will allocate memory for currently visible cells and reuse the other ones.
Upvotes: 2