5

I'm using SDWebImage to download image async from server.Some images on server are of size ~1.5 to 2. MB.I'm showing these images on UICollectionView. I'm getting memory warning and app crashes after several run.Some times it happens when images downloaded first time and some time when i scroll the collection view up and down. Here is my code-

-(void)setImageWithUrl:(NSURL*)imgurl onImageView:(UIImageView*)image prograssive:(BOOL)progressive
{
    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = image;
    SDWebImageOptions opt;
    if (progressive) {

        opt = SDWebImageProgressiveDownload;
    }
    else
        opt = SDWebImageRetryFailed;
    [image sd_setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:@"default_image"] options:opt progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
         if (!activityIndicator)
         {
             [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
             activityIndicator.center = CGPointMake(weakImageView.frame.size.width /2, weakImageView.frame.size.height/2);
             // activityIndicator.center = weakImageView.center;
             [activityIndicator setBackgroundColor:[UIColor clearColor]];
             [activityIndicator startAnimating];
         }
     }
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
     {
         [activityIndicator removeFromSuperview];
         activityIndicator = nil;
     }];
}

and in AppDelegate-

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{

    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] cleanDisk];
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
}

Please not the images are high resolution(more than 3000 pixel width).For example one of them has 4256*2832 and size 979KB produce the memory warning.Any help or suggestion would be appreciated. EDIT:- my App is being killed due to memory pressure, but only on iPhone 4s and lower versions(working fine on iPhone5 and above).when a large image is being downloaded(or complete download) i got a sudden memory spike(it goes from ~25mb to ~90mb) and app crashes.Any suggestion what to do?

Bharat
  • 2,987
  • 2
  • 32
  • 48
  • it's much easier to help if you show where it does crash and if you can post the revenant information from the debugger when you encounter the crash (crash log with stack trace) – Volker Jan 05 '15 at 13:28
  • 1
    No crash log only a message window appears in Xcode that app stopped unexpectedly(iOS quit the app due to high memory pressor) – Bharat Jan 05 '15 at 13:34
  • I post this issue on SDWebImage Github page. To get the attention from it's developer. – Asif Bilal Jul 09 '15 at 17:13

2 Answers2

3

It sounds like your App is being killed due to memory pressure. Check Instruments where exactly the memory increases and maybe try using an Autorelease Pool to mitigate any memory spikes.

dlinsin
  • 19,249
  • 13
  • 42
  • 53
  • Yes, my App is being killed due to memory pressure, but only on iPhone 4s and lower versions.when a large image is downloaded i got a sudden memory spike(from ~25mb to ~90mb) and app crashes.Any suggestion to handle this. – Bharat Jan 06 '15 at 04:35
  • As I suggested, using an Autorelease Pool will help handling memory spikes. Usually, you wrap the allocation of the image, especially if you allocate a couple of images in a short period e.g. in a loop. – dlinsin Jan 06 '15 at 10:12
  • I used Autorelease and now app crashes after several scrolls.Now working on some image optimisation algorithms on server. Anyway your suggestion helped me..thanks a ton. – Bharat Jan 06 '15 at 12:15
3

Yes, @dlinsin is absolutely right. This is definitely memory issue.

I hope you are not using that same 2mb image as a thumbnail. if yes then thats why you are crashing. Please use lower resolution images as thumbs and show the larger ones when the user asks for it

By the way, use this to see if that could any help.

[[SDImageCache sharedImageCache] setShouldDecompressImages:NO]; [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];

Asif Bilal
  • 4,309
  • 3
  • 17
  • 27
  • These are the singleton class methods. So, this code should be written somewhere in application configurations as per application's architecture. Normally, developers write this kind of code in AppDelegate.swift file in: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool – Asif Bilal May 13 '20 at 13:20