2012-07-11 58 views
0

我要求你在我的应用程序的内存问题的帮助!低内存警告与几个EGOImageView

我在表格中有一个单元格,其中包含一个或多个EGOImageView s的水平滚动视图。此单元格始终位于第0行,但我经常需要用另一个Image重新加载此单元格。

的问题是:当我重装我的手机太多的时间(30或40倍),我收到一个内存警告或时有时无的错误和应用程序崩溃...

我使用ARC国防部。这里是我的代码预览:

单元代码为行0

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

你有任何想法,我的错误是

- (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]; 
} 

表的代码?当我重新加载我的细胞时,是否必须手动处理我的EGOImageView并将它们放到nil

谢谢你的时间!

回答

1

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

首先,检查它是否释放_responseData在EGOImageLoadConnection的dealloc中,这样它看起来像:

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

    [super dealloc]; 
} 

重新分配它使用它,无其图像属性(记得视图时,然后调用cancelImageLoad,我使用ARC代码示例):

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

而且我也建议你有时候打电话:

[EGOCache.currentCache clearCache]; 

无论如何,随意使用仪器。打开分配工具,单击虚拟机跟踪器并将自动快照设置为1秒延迟。然后观看脏内存列和内存标签70行,它显示了您的应用程序中消耗了多少内存映像。

+0

谢谢你的回答,我明天会测试它并给你我的反馈! – palmitoto 2012-08-07 17:36:08