2013-02-10 122 views
0

在我的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]; 
+0

您应该使用表格视图,而不是图片的滚动视图。这样,您只能加载屏幕上可见的图像,而不是每个图像。当表格滚动时,单元格被重用,并且从数组中填充它们,就像表格的任何其他数据一样(但数组应该包含图像名称,而不是实际图像,因此图像仅在您将他们在桌子上)。 – rdelmar 2013-02-10 17:39:45

+0

仅供参考,您应该阅读“延迟加载” - 要点是仅根据需要加载资产以避免内存不足。你可能想看看我在这个关于UIScrollView和延迟加载的答案中发布的代码:http://stackoverflow.com/a/14661487/840992 – 2013-02-10 20:31:53

回答

0

它看起来像你所访问的索引0..200图像阵列,但它仅包含三个元素。任一组numberOfViews = 3;,或索引到阵列是这样的:

ImageView.image = images[i%3]; 

模数索引将导致你添加每三个图像中的重复序列,并且仍然让您与滚动视图的子视图的更大数量的测试。我怀疑你的崩溃是关于只有200张图像的内存,除非它们是超大的。

+0

让我们使用新的酷语法'ImageView.image = images [i%3 ];':) – asdf 2013-02-10 17:31:23

+0

是的,我也会编辑以添加更酷的属性语法。 :)也@NightHunter - 建议您在堆栈变量名称(imageView,而不是ImageView)上使用小写字母。 – danh 2013-02-10 17:33:04

+0

@danh,感谢您的快速回复,我不是说只有三张图片。有200张图片。 UIImage * image1 = [UIImage imageNamed:@“Image-001.jpg”]; UIImage * image2 = [UIImage imageNamed:@“Image-002。jpg“]; UIImage * image200 = [UIImage imageNamed:@”Image-200.jpg“];总共200张图片大小为300Mb(每张图片大约2Mb),我减小了它们的大小。 。我应该怎么做? – 2013-02-10 17:37:02

0

您应该使用表格视图来显示图像。你不想创建所有这些图像的数组,因为这将所有这些图像放入记忆中 - 这不是一件好事。你甚至不需要创建一个数组来保存这些名称,因为它们的名字可以很容易地用一个整数(你可以从tableView:cellForRowAtIndexPath:方法中的indexPath.row获得)构造出来。像这样的:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 200; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    NSString *imageName = [NSString stringWithFormat:@"Image-%.3d.jpg",indexPath.row +1]; 
    UIImage *image = [UIImage imageNamed:imageName]; 
    cell.imageView.image = image; 
    return cell; 
} 

这个例子只是使用单元格的默认imageView来显示图像,这是非常小的。您可能想要创建一个自定义单元格,其中包含更大的图像视图(如果每行需要多个图像,则并排配对)。

+0

感谢您的帮助。我真的想使用表格视图。但是,我的应用程序几乎完成大约90%,只是有这样的错误,我没有考虑我的应用程序的内存管理,我认为它不是我对我来说。但是,现在我陷入困境。我不想从头开始。请给我一些建议,我如何重新编码我的代码。再次感谢。 – 2013-02-10 18:13:45

+0

@NightHunter,我不知道这是否会有所帮助(取决于内存问题),但摆脱那些你创建图像的200行,并摆脱阵列。当你在你的循环中调用setImage时,使用上面的代码(imageName = ...和image = ...)来填充你的图像视图(当你创建imageName时,你将使用我而不是indexPath.row)。 – rdelmar 2013-02-10 18:18:14