由于ios sdk中的内存问题导致应用程序崩溃

时间:2013-05-29 14:03:55

标签: iphone ios objective-c xcode memory

在我的应用中,我使用图片加载表格视图。有一个删除按钮可以删除图像。

当图片尺寸太大(例如1.5MB)时,应用程序因内存不足警告而崩溃。

我如何使用乐器或其他解决方案来解决这个问题?

如何在cellforRowIndexPath方法中释放对象。

我应该启用ARC项目吗?

我是否启用ARC Enabled项目会产生什么影响?

我的cellRow代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    for (UIView *v in [cell.contentView subviews]) {
        [v removeFromSuperview];
    }

    int section = indexPath.section;

    sectionItems = [sections objectAtIndex:section];

    int n = [sectionItems count];

    //NSLog(@"section items -%d",n );

    int  i=0;
    int i1=0;

    while(i<n)
    {
        int yy = 3 +i1*100;
        int j=0;
        for(j=0; j<3;j++){

            if (i>=n) break;
            Item *item = [sectionItems objectAtIndex:i];            

            //Imageview
            UIImageView *pp=[[UIImageView alloc]initWithFrame:CGRectMake(20+215*j, yy*2.3, 190, 190)];
            pp.image=item.img;
            //NSLog(@"%@ %@",item.img,item.image);
            //pp.backgroundColor=[UIColor redColor];
            [cell.contentView addSubview:pp];

            //Image Button
           // CGRect rect1 = CGRectMake(30+215*j, yy*2.3, 190, 250);
            CGRect rect1 = CGRectMake(20+215*j, yy*2.3, 190, 190);
            UIButton *button1=[[UIButton alloc] initWithFrame:rect1];
            [button1 setFrame:rect1];
            NSString *tagValue1 = [NSString stringWithFormat:@"%d%d", indexPath.section+1, i];
            button1.tag = [tagValue1 intValue];
            [button1 setBackgroundImage:nil forState:UIControlStateNormal];
            [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
           // button1.backgroundColor=[UIColor greenColor];
            [cell.contentView addSubview:button1];

           //delete button..16 03 2013
            CGRect rect2 = CGRectMake(176+215*j, yy*2.3, 30, 30);
            UIButton *button2=[[UIButton alloc] initWithFrame:rect2];
            [button2 setFrame:rect2];
            NSString *tagValue2 = [NSString stringWithFormat:@"%d%d", indexPath.section+1, i];
            button2.tag = [tagValue2 intValue];
            [button2 setBackgroundImage:[UIImage imageNamed:@"can.png"] forState:UIControlStateNormal];
            [button2 addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
            //button2.backgroundColor=[UIColor blackColor];


            [cell.contentView addSubview:button2];
            if (isDeleting == YES)
            {
                [button2 setHidden:NO];
                [button1 setUserInteractionEnabled:NO];
            }
            else
            {
                [button1 setUserInteractionEnabled:YES];
                [button2 setHidden:YES];
            }


            i++;
        }
        i1 = i1+1;
    }

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    return cell;
}

2 个答案:

答案 0 :(得分:0)

mmm ......好吧,首先:

pp 
button1
button2

已分配且从未发布过...... 在您将它们作为子视图添加之后,开始为所有这些版本添加一个版本:

[pp release];
[button1 release];
[button2 release];

你当前的代码是每次用户滚动时分配大量内存,而且从不发布...... 这可能会解决你的问题...

进一步的步骤应该是重用对象,学习如何重用所有按钮和imageView(只为它们设置新内容,作为图像,而不是删除它们并重新创建它们;我知道单元格可以有不同数量的按钮。 ..但我相信你可以管理它..只删除过多的物体,可能会表现得更好)

答案 1 :(得分:0)

而不是

for (UIView *v in [cell.contentView subviews]) {
        [v removeFromSuperview];
    }

试试这个

[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

我认为您的编码是在UIView * v中复制视图并从v中删除superView而不是[cell.contentView subViews]

相关问题