gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:53:59 25 4
gpt4 key购买 nike

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

我正在使用 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 委托(delegate)方法

 - (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 类中,但问题还是一样。

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

最佳答案

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

 UIGraphicsEndImageContext();

之后

image = UIGraphicsGetImageFromCurrentImageContext();

关于ios - UICollectionView 中滚动或重新加载数据时出现内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24139683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com