在我的ipad應用程序中,我有200個圖像,並將這些圖像添加到數組中。
然後通過循環將此數組添加到圖像視圖中。 然後,我將這個圖像視圖添加爲滾動視圖的子視圖。添加新圖像並從圖像視圖中刪除舊圖像
當我打開應用程序時,我的應用程序崩潰。
我嘗試縮小圖像大小。但是,它沒有奏效。
我的一個朋友先告訴我應該只添加image1和image2。
當用戶滾動image1時,它顯示image2。
之後image1從圖像視圖中刪除。
然後將圖像3添加到圖像視圖。
他告訴它可以保持內存使用。
但是,我不知道我該怎麼做? :D
請給我舉個例子。
在此先感謝。
我的代碼是在這裏,
- (void)viewDidLoad
{
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:@"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:@"Image-002.jpg"];
UIImage *image200 = [UIImage imageNamed:@"Image-200.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,...,image200,nil];
NSInteger numberOfViews = 200;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:ScrollView];
您應該使用表格視圖,而不是圖片的滾動視圖。這樣,您只能加載屏幕上可見的圖像,而不是每個圖像。當表格滾動時,單元格被重用,並且從數組中填充它們,就像表格的任何其他數據一樣(但數組應該包含圖像名稱,而不是實際圖像,因此圖像僅在您將他們在桌子上)。 – rdelmar 2013-02-10 17:39:45
僅供參考,您應該閱讀「延遲加載」 - 要點是僅根據需要加載資產以避免內存不足。你可能想看看我在這個關於UIScrollView和延遲加載的答案中發佈的代碼:http://stackoverflow.com/a/14661487/840992 – 2013-02-10 20:31:53