Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 添加新图像并从图像视图中删除旧图像_Ios_Xcode_Ipad_Cocoa Touch - Fatal编程技术网

Ios 添加新图像并从图像视图中删除旧图像

Ios 添加新图像并从图像视图中删除旧图像,ios,xcode,ipad,cocoa-touch,Ios,Xcode,Ipad,Cocoa Touch,在我的ipad应用程序中,我有200张图像,我将这些图像添加到一个数组中。 然后通过循环将该数组添加到图像视图中。 然后,我添加这个图像视图作为滚动视图的子视图 当我打开应用程序时,我的应用程序崩溃。 我尝试缩小图像大小。但是,它不起作用 我的一位朋友告诉我首先应该只添加image1和image2。 当用户滚动图像1时,它会显示图像2。 之后,从图像视图中删除图像1。 并将image3添加到图像视图中 他说它可以保持内存使用。 但是,我不知道我该怎么做D 请给我举个例子。 提前谢谢 我的代码在这

在我的ipad应用程序中,我有200张图像,我将这些图像添加到一个数组中。
然后通过循环将该数组添加到图像视图中。 然后,我添加这个图像视图作为滚动视图的子视图

当我打开应用程序时,我的应用程序崩溃。
我尝试缩小图像大小。但是,它不起作用

我的一位朋友告诉我首先应该只添加image1和image2。
当用户滚动图像1时,它会显示图像2。
之后,从图像视图中删除图像1。
并将image3添加到图像视图中

他说它可以保持内存使用。
但是,我不知道我该怎么做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];
-(void)viewDidLoad
{
[超级加载视图];
self.view.backgroundColor=[UIColor grayColor];
UIScrollView*ScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,44,self.view.frame.size.width,self.view.frame.size.height)];
ScrollView.PaginEnabled=是;
//创建一个UIImage以保存Info.png
UIImage*image1=[UIImage ImageName:@“Image-001.jpg”];
UIImage*image2=[UIImage ImageName:@“Image-002.jpg”];
UIImage*image200=[UIImage ImageName:@“Image-200.jpg”];
NSArray*images=[[NSArray alloc]initWithObjects:image1,image2,…,image200,nil];
NSInteger numberOfViews=200;
对于(int i=0;i
看起来您正在访问索引为0..200的图像数组,但它只包含三个元素。可以设置
numberOfViews=3;
,也可以像这样索引到数组中:

ImageView.image = images[i%3];
- (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;
}

模索引将使您以重复的顺序添加三个图像中的每一个,并且仍然允许您使用更多的scrollView子视图进行测试。我怀疑您的崩溃与只有200个图像的内存有关,除非它们是超大图像。

您应该使用表视图来显示图像。您不想创建一个所有这些图像,因为这会将所有这些图像放入内存——这不是一件好事。您甚至不需要创建一个数组来保存这些名称,因为它们的名称可以很容易地从整数构造(可以从tableView:cellforrowatinexpath:method中的indexath.row获得)。类似于这样:

ImageView.image = images[i%3];
- (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;
}

此示例仅使用单元格的默认图像视图来显示图像,图像非常小。您可能希望创建一个自定义单元格,其中包含一个较大的图像视图(如果您希望每行有多个图像,则可以并排创建一对图像).

是的,我也会编辑以添加更酷的属性语法。:)另外@NightHunter-建议您在堆栈变量名上使用小写形式(imageView,而不是imageView)。@danh,感谢您的快速回复,我不是说只有三个图像。共有200个图像。UIImage*image1=[UIImage ImageName:@“image-001.jpg”];UIImage*image2=[UIImage ImageName:@“Image-002.jpg”];UIImage*image200=[UIImage ImageName:@“Image-200.jpg”];总共200个图像大小为300Mb。(每个图像大约有2Mb)。我减小了它们的大小。但是,没有起作用。我该怎么办?所以你有200行源代码来创建这些图像,然后用200+nil参数列表初始化数组?我敢打赌那里肯定有复制/粘贴错误。请粘贴控制台在崩溃时说的内容。@danh,我想显示我的所有代码。但是,注释有字符限制。W当我运行我的应用程序时,我看不到任何图像,只能在视图上看到灰色屏幕。我认为我的编码占用了大量内存。我想创建“添加新图像”和“删除旧图像”。我如何创建它?您可能应该使用表视图,而不是滚动视图来创建图像。这样,您只加载屏幕上可见的图像,而不是所有图像。如图所示表格滚动,单元格重复使用,您可以从数组中填充它们,就像表格的任何其他数据一样(但数组应该包含图像名称,而不是实际图像,因此图像只有在放入表格时才会实例化)。作为参考,您应该阅读“延迟加载”部分-要点是只根据需要加载资产,以避免内存不足。你可能想看看我在回答中发布的关于UIScrollView和延迟加载的代码:谢谢你的帮助。我真的很想使用table view。但是,我的应用程序几乎完成了90%左右。只是有这样的错误。我没有考虑内存管理我的应用程序。我以为它不适合我。但是,现在我有麻烦了。我不想从头再来。请给我一些建议,如何重新编码我的代码。再次感谢。@NightHunter,我不知道这是否有帮助(取决于内存问题),但要去掉创建图像的200行,也要去掉数组。在循环中调用setImage时,请使用上面的代码(imageName=…和image=…)填充图像视图(创建imageName时使用I而不是indexath.row)。