gpt4 book ai didi

ios - 多个 EGOImageView 出现低内存警告

转载 作者:行者123 更新时间:2023-11-29 04:33:00 25 4
gpt4 key购买 nike

我请求您帮助解决我的应用程序中的内存问题!

我在表格中有一个单元格,其中包含一个带有一个或多个 EGOImageView 的水平 ScrollView 。该单元格始终位于第 0 行,但我经常需要使用另一个图像重新加载该单元格。

问题是:当我重新加载单元格次数过多(30 或 40 次)时,我会收到内存警告或有时没有错误,并且应用程序崩溃...

我使用 ARC 模组。这是我的代码的预览:

单元格代码

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier animated:(BOOL)animated{       
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self.contentView setFrame:CGRectMake(0, 0, 320, 240)];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 240)];
[self.scrollView setDelegate:self];
[self.scrollView setPagingEnabled:YES];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
self.scrollView.contentSize = CGSizeMake(320, 240);

pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 220, 320, 20)];
[pageControl setCurrentPage:0];
[self.contentView addSubview:self.scrollView];
[self.contentView addSubview:pageControl];
isAnimated= animated;


return self;
}


- (void)configureGallery:(NSInteger)accommodationId{

counter =0;
Accommodation *item = [[[DataManager sharedManager]accommodations]objectAtIndex:accommodationId];
numberPictures = [[item photo_set]count];

if (numberPictures >1) {
[scrollView setContentSize:CGSizeMake((numberPictures + 2)*320, 240)];
} else [scrollView setContentSize:CGSizeMake(320, 240)];

[pageControl setNumberOfPages:numberPictures];

// add the last image into the first position
if (numberPictures) {
[self addImageWithName:[NSString stringWithFormat:@"%@%@",[[DataManager sharedManager]baseUrl],[[item.photo_set objectAtIndex:numberPictures-1]thumbnail_gallery]] atPosition:0];
}


// add all of the images to the scroll view
for (int i = 0; i < numberPictures; i++) {
[self addImageWithName:[NSString stringWithFormat:@"%@%@",[[DataManager sharedManager]baseUrl],[[item.photo_set objectAtIndex:i]thumbnail_gallery]] atPosition:i+1 ];
}

// add the first image into the last position
[self addImageWithName:[NSString stringWithFormat:@"%@%@",[[DataManager sharedManager]baseUrl],[[item.photo_set objectAtIndex:0]thumbnail_gallery]] atPosition:numberPictures+1];

[self.scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO];
[self performSelector:@selector(switchToNextPhoto) withObject:nil afterDelay:5];


}

- (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
// add image to scroll view
UIImageView * placeHolderView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"France"]];
[placeHolderView setFrame:CGRectMake(position*320, 0, 320, 240)];
UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake((position*320)+160, 120, 20, 20)];
[activityView startAnimating];
EGOImageView *imageView = [[EGOImageView alloc] init];


imageView.frame = CGRectMake(position*320, 0, 320, 240);
[self.scrollView addSubview:placeHolderView];
[self.scrollView addSubview:activityView];
[self.scrollView addSubview:imageView];

imageView.imageURL = [NSURL URLWithString:imageString];
}

第 0 行的表代码

GalleryDetailCell *cell = (GalleryDetailCell *) [tableView dequeueReusableCellWithIdentifier: @"GalleryAnimated"];
if (cell == nil) {
cell = [[GalleryDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"GalleryAnimated" animated:animated];
}
[cell configureGallery:id];
cellToReturn = cell;

你知道我的错误在哪里吗?当我重新加载单元格时,我是否必须手动处理我的 EGOImageView 并将它们设置为 nil??

感谢您的宝贵时间!

最佳答案

EGOImageView 可能会导致内存泄漏,请注意。

首先,检查EGOImageLoadConnection的dealloc中是否释放了_responseData,如下所示:

- (void)dealloc
{
self.response = nil;
self.delegate = nil;
[_connection release];
[_imageURL release];
[_responseData release]; //this line is absent in current EGOImageLoadConnection.m

[super dealloc];
}

然后在释放使用它的 View 时调用cancelImageLoad并将其图像属性置零(记住,我使用ARC代码示例):

 - (void)dealloc
{
//...
self.myView.image = nil;
[self.myView cancelImageLoad];
//...
}

我还建议有时打电话:

[EGOCache.currentCache clearCache];

无论如何,请随意使用 Instruments。打开分配工具,单击 VM Tracker 并将自动快照设置为 1 秒延迟。然后观察“脏内存”列和“内存标签 70”行,它显示了应用程序中镜像消耗了多少内存。

关于ios - 多个 EGOImageView 出现低内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437497/

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