0

アプリのメモリの問題で助けを求めます!

1 つ以上EGOImageViewの s を持つ水平スクロールビューを含むテーブルにセルがあります。このセルは常に行 0 にありますが、このセルを別の画像でリロードする必要があることがよくあります。

問題は、セルを何度も (30 回または 40 回) リロードすると、メモリ警告が表示されるか、エラーが発生せずにアプリがクラッシュすることです...

ARC mod を使用しています。ここに私のコードのプレビューがあります:

セルコード

- (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

お時間をいただきありがとうございます!

4

1 に答える 1

1

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

次に、それを使用するビューの割り当てを解除し、その画像属性をゼロにするときにcancelImageLoadを呼び出します(ARCコードサンプルを使用することを忘れないでください)。

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

また、時々電話することをお勧めします:

[EGOCache.currentCache clearCache];

とにかく、Instrumentsを自由に使用してください。Allocations Instrumentを開き、VM Trackerをクリックして、自動スナップショットを1秒の遅延に設定します。次に、[ダーティメモリ]列と[メモリタグ70]行を確認します。これは、アプリケーションでメモリイメージがどれだけ消費しているかを示しています。

于 2012-07-18T15:52:45.153 に答える