veereev
veereev

Reputation: 2688

I get didreceivememorywarning uitableview usuing AFNetworking to download the images?

Hello everyone I have a UITableView which I use AfNetworking to load very large images. But however when the didreceivememorywarning is hit my app just crashes. Here is my code :

In AFNetworking+uiimageview.m I have added this as suggested :

@implementation AFImageCache

- (id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(didReceiveMemoryWarning)
                                                     name:UIApplicationDidReceiveMemoryWarningNotification
                                                   object:nil];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    DebugLog(@"AFNetworking DID RECEIVED MEMORY WARNING ");
    [self removeAllObjects];
}

And my code in uitableview is as follows :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        TousCell *tvCell;
        NSArray *nib;
        nib = [[NSBundle mainBundle] loadNibNamed:@"TousCell" owner:self options:nil];
        tvCell = [nib objectAtIndex:0];

        tvCell.lbltitle.text=[[dict valueForKey:@"message"]objectAtIndex:i];

[tvCell.imgPhoto setImageWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://test.com/%@", [[dict valueForKey:@"photo"]objectAtIndex:i]]]];

May be I am recreating the cells each and everytime? But I am sure its linked to the images download as the images are v large sometimes ( in size about 2 - 3 Mb?).

Upvotes: 1

Views: 662

Answers (2)

viggio24
viggio24

Reputation: 12366

You're recreating the table cell each time so if images are different they will take a lot of space. You must reuse table view cells as much as possible using the

[tableView dequeReusableCellWithIdentifier:]
method and registering your nib in the view controller view did load using the:

[tableView registerNib:forCellReuseIdentifier:]

method.

Also note that if the images are large, AFNetworking offers you the possibility to downsize them before returning to the caller. This is especially useful if you need to fit a large image on a small cell (e.g. the one from a table view). Note that AFNetworking doesn't provide the resizing functions but returns the image in two different blocks: one before post-processing and one after post-processing.

Upvotes: 1

Toploulou
Toploulou

Reputation: 1220

2 or 3 MB is way too large for a thumbnail picture in a UITableViewCell. And you are re - creating a cell each time. You should use

[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

to improve performance

Upvotes: 1

Related Questions