【问题标题】:Memory leak issue while scrolling or reloadData in UICollectionView在 UICollectionView 中滚动或重新加载数据时出现内存泄漏问题
【发布时间】:2014-06-10 11:19:05
【问题描述】:

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

我正在使用 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 、 scroll 和按下 Cell 内的 UIButton 并更新特定的 Cell 索引时,内存会增加 1、1.5 或一段时间,最高可达 2.5 MB。

编辑

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

我不知道问题出在哪里,请帮我找出问题所在。

【问题讨论】:

  • 请包含SearchResult 方法的其余代码(应以小写s 开头)。提供的代码看起来不错,看起来应该不会导致任何类型的内存泄漏。
  • @nhgrif 我更新了我的问题

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


【解决方案1】:

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

 UIGraphicsEndImageContext();

之后

image = UIGraphicsGetImageFromCurrentImageContext();

【讨论】:

  • 另外,如果你不在单元格中显示图像视图,你有同样的问题吗?这可能是单元重用问题。
  • 伟人……非常感谢它对我有用,并向你致敬这 supperrbb 行代码。两天后,我将开始在石头上挠头。你救了我的命。
猜你喜欢
  • 1970-01-01
  • 2012-03-22
  • 2017-02-28
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 2014-06-11
  • 1970-01-01
相关资源
最近更新 更多