Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27167

AFNetworking low memory issue

I download image using this code:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SECustomCollectionViewCell *collectionViewCell = (SECustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"SECustomCollectionViewCell" forIndexPath:indexPath];

    NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];

    collectionViewCell.theImageView.image = nil;

    if (artwork[@"video_url"])
    {
        UIWebView *webView = (UIWebView *)[collectionViewCell.contentView viewWithTag:100];
        NSString * html = [self embedYouTube:artwork[@"video_url"] frame:collectionViewCell.frame];

        [webView setHidden:NO];

        [webView loadHTMLString:html baseURL:nil];

        [collectionViewCell.activityIndicator setHidden:YES];
    }
    else
    {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:artwork[@"image_url"]]];

        UIImage *cachedImage = [[[UIImageView class] sharedImageCache] cachedImageForRequest:request];

        if (cachedImage)
        {
            collectionViewCell.theImageView.image = [UIImage scaleImage:cachedImage toWidth:collectionViewCell.frame.size.width];
            [collectionViewCell.activityIndicator setHidden:YES];
        }
        else
        {
            [collectionViewCell.theImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                // Only update visible cell, to avoid inserting image to another cell.
                SECustomCollectionViewCell *visibleCollectionViewCell = (id)[collectionView cellForItemAtIndexPath:indexPath];

                if (visibleCollectionViewCell)
                {
                    [visibleCollectionViewCell.theImageView setImage:[UIImage scaleImage:image toWidth:collectionViewCell.frame.size.width]];

                    [visibleCollectionViewCell.activityIndicator stopAnimating];

                    [visibleCollectionViewCell.activityIndicator setHidden:YES];
                }

            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

            }];
        }
    }

    return collectionViewCell;
}

but it cause low memory issues.

Upvotes: 2

Views: 148

Answers (1)

zaph
zaph

Reputation: 112855

It is not AFNetworking but the image code that is causing the memory usage. JPEG compresses images but when an image is created there will be 4-bytes per pixel. Since the jpeg file on server that is 1.4MB that is all AFNetworking will load.

It seems you are using some helper class look at that code and NSLog the size of the actual data that AFNetworking downloads.

An image of 5407 × 3605 at 4 bytes per pixel will create an image of over 77MB. You can scale it but first the original image is rendered and the scaling will use more memory because at the end you will have two images.

You need to wrap the creation of the original image and the scaling in an autorelease pool so the original is released as soon as possible.

Best is not to load such a large image in the first place.

Upvotes: 2

Related Questions