在UICollectionView中滚动或重新加载数据时出现内存泄漏问题

时间:2014-06-10 11:19:05

标签: ios objective-c memory-management memory-leaks uicollectionview

经过长时间的搜索,我发现了这一点 关于这个主题的所有主题或讨论都没有完全满足我的要求,或者可能在某些我不了解如何正确使用它的地方。

我正在使用ARC,而且我也自动阅读 CollectionView 版本,但在我的情况下它不是。

到目前为止,这是我的代码。

自定义单元格实施

@implementation MyCell
@synthesize cellQty, cellBtnPlus, cellBtnMinus, myImageView, lbl1, lbl2, lbl3, lbl4;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor=[UIColor whiteColor];
        self.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        self.layer.borderWidth = 2.0;
        self.layer.cornerRadius = 8.0;
        myImageView = [[UIImageView alloc] init];
        myImageView.tag = 15;
        [self.contentView addSubview:self.myImageView];

        CGRect btn1Rect = CGRectMake(190, 230 , 35 , 35);
        cellBtnPlus = [[UIButton alloc] initWithFrame:btn1Rect];
        cellBtnPlus.tag=11;
        [self.contentView addSubview:self.cellBtnPlus];

        CGRect btn2Rect = CGRectMake(105, 230 , 35 , 35);
        cellBtnMinus = [[UIButton alloc] initWithFrame:btn2Rect];
        cellBtnMinus.tag=12;
        [self.contentView addSubview:self.cellBtnMinus];

        CGRect txt1Rect = CGRectMake(143, 230 , 45 , 30);
        cellQty = [[UILabel alloc] initWithFrame:txt1Rect];
        cellQty.tag=13;
        [self.contentView addSubview:self.cellQty];

        CGRect lbl1Rect = CGRectMake(10, 28, 215, 35);              // Description
        lbl1 = [[UILabel alloc] initWithFrame:lbl1Rect];
        lbl1.tag=1;
        [self.contentView addSubview:lbl1];

        // There are three more UILabels as above

        lbl1 = (UILabel *)[self viewWithTag:1]; [lbl1 setText:@"Label1"];
        lbl2 = (UILabel *)[self viewWithTag:2]; [lbl2 setText:@"Label2"];
        lbl3 = (UILabel *)[self viewWithTag:3]; [lbl3 setText:@"Label3"];
        lbl4 = (UILabel *)[self viewWithTag:4]; [lbl4 setText:@"Label4"];
    }
    return self;
}

在主类实现中,按下按钮

- (IBAction)SearchResult
{
    productID   = [[NSMutableArray alloc] init];
    desc        = [[NSMutableArray alloc] init];
    qty         = [[NSMutableArray alloc] init];
    price       = [[NSMutableArray alloc] init];
    code        = [[NSMutableArray alloc] init];
    barcodes    = [[NSMutableArray alloc] init];
    taxRate     = [[NSMutableArray alloc] init];
    costPrice   = [[NSMutableArray alloc] init];
    NSString sqliteQuery = @"my query here to select data from sqlite database";
    if (sqlite3_open(database, &dbConnection) == SQLITE_OK)
    {

        if (sqlite3_prepare_v2(dbConnection, [sqliteQuery UTF8String], -1, &sQLStatement, NULL) == SQLITE_OK)
        {
            while  (sqlite3_step(sQLStatement) == SQLITE_ROW)
            {
                zero = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 0)];
                one = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 1)];
                two = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 2)];
                three = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 3)];
                four = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 4)];
                five = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 5)];
                six = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 6)];
                seven = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(sQLStatement, 7)];

                [productID addObject:zero];
                [barcodes addObject:one];
                [code addObject: two];
                [desc addObject:three];
                [price addObject:four];
                [qty addObject:five];
                [taxRate addObject:six];
                [costPrice addObject:seven];
            }
            sqlite3_finalize(sQLStatement);
        }
        else
        {
            NSLog(@"Error %s ", sqlite3_errmsg(dbConnection));
        }
        sqlite3_close(dbConnection);

    [myCollection reloadData];
}

CollectionView委托方法

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];

 [[cell cellBtnPlus] addTarget:self action:@selector( btnPlus:event:) forControlEvents:UIControlEventTouchUpInside];

 [[cell cellBtnMinus] addTarget:self action:@selector(btnMinus:event:) forControlEvents:UIControlEventTouchUpInside];

 cell.cellQty.text = @"0";

 for (i = 0; i < [updatedCodes count]; i++)
 {
     if ([[code objectAtIndex:indexPath.row] isEqualToString:[updatedCodes objectAtIndex:i]])
    {
        cell.cellQty.text = [updatedQty objectAtIndex:i];
    }
}
 NSString *imageName =[[productID objectAtIndex:indexPath.row] stringByAppendingString:@"img.jpg"]; 
 getImagePath = [documentsDirectory stringByAppendingPathComponent:imageName]; // images are saved in Document Directory.
 image = [UIImage imageWithData:[NSData dataWithContentsOfFile:getImagePath]];
 if (image == nil)
{
    cell.myImageView.frame = CGRectMake(35, 60, 160, 160);
    cell.myImageView.image = noPic; // a blank Picture.
}
else
{
    if (image.size.width == image.size.height)
    {
        newSize = CGSizeMake(160, 160);
    }
    if (image.size.width < image.size.height)
    {
        // set size after calculation as **newSize**
    }
    else
    {
        // set size after calculation as **newSize**
    }
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    image = UIGraphicsGetImageFromCurrentImageContext();
    if (image.size.width == image.size.height)
    {
        imgRect = CGRectMake(35, 60, 160, 160);
    }
    if (image.size.width < image.size.height)
    {
        imgRect = CGRectMake(y, 60, image.size.width, 160);
    }
    if (image.size.width > image.size.height)
    {
        imgRect = CGRectMake(37, y, 160, image.size.height);
    }
    cell.myImageView.frame = imgRect;
    cell.myImageView.image = image;
 }
 cell.lbl1.text = [desc objectAtIndex:indexPath.row];
 cell.lbl2.text = [qty objectAtIndex:indexPath.row];
 cell.lbl3.text = [price objectAtIndex:indexPath.row];
 cell.lbl4.text = [code objectAtIndex:indexPath.row];
 return cell;
}

每个 reloadData 滚动,按下Cell内的UIButtons并更新特定的Cell索引内存增加1,1.5或一些时间增加到2.5 MB。

修改

我还尝试在SearchResult方法中使用 @autoreleasepool ,在CellForItemAtIndexPath方法中,在Cell Custom类中,但问题是同样的。

我不知道问题出在哪里,请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

我不确定这是否是您问题的原因,但您应该致电

 UIGraphicsEndImageContext();

之后

image = UIGraphicsGetImageFromCurrentImageContext();