我有图像的滚动视图,但如果我添加许多图像(> 10),应用程序崩溃(内存限制)。我使用的是iPad 4,不知道为什么会这样。你能帮忙修理一下吗?我的代码是:
- (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;
答案 0 :(得分:2)
您的应用崩溃,因为每台设备的内存量都有限。 问题是您尝试一次加载所有图像。 相反,尝试UICollectionView或UITableView - 它们将为当前可见的单元格分配内存并重用其他单元格。