在我的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