0

I have a very strange problem . I have a scrollView, in which i load images that are taken from the web. it works great on the iPad, and the memory is stable and low , one 35MB .

Every page i load only the relevant images and remove the rest by setting UIImageView=nil.

The problem starts when the images that i load into the scroller are not coming from the web, but pre loaded from disk,than as long as i scroll more, the memory rise up and up from 30 to 200. It seems when the image is not from the web( offline mode) , he wouldnt release the images, but when its online mode, everything works perect .

Here is how i check offline or online to load the image to the scroller :

//this method gets: or image url string(online), or UIImage(offline).
[self.imageOperationQueue addOperationWithBlock:^
 {

     UIImage *image ;
     if(!isOffLineMode)
         image=[UIImage imageWithData:[NSData dataWithContentsOfURL:userUrl] scale:1.0];
     else
         image=[dic objectForKey:@"userImage"];

     if (image)
     {
         UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
         [image drawAtPoint:CGPointZero];
         image = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();
     }



     if (image != nil)
     {             
         [[NSOperationQueue mainQueue] addOperationWithBlock:^
          {

               UIImageView *view=thisCell.userImage;
              view.image=nil;
              view.image=image;

          }];
         image=nil;

     }
 }];

In both cases i clear images with imageview.image=nil;. Problems occur only in offline mode .

Rashad
  • 11,057
  • 4
  • 45
  • 73
  • In offline mode you have `dict` is this holding onto all the images? – Paul.s Jun 01 '14 at 10:56
  • @Paul.s its like that : NSDictionary *dic=[mainData objectAtIndex:index]; where main data holds all the images, and the dic is holds only 1 image. BUT , i clear the dic at the end of the block with dic=nil; .. –  Jun 01 '14 at 11:03

1 Answers1

0

Problem is in here:

if(!isOffLineMode)
     image=[UIImage imageWithData:[NSData dataWithContentsOfURL:userUrl] scale:1.0];
else
     image=[dic objectForKey:@"userImage"];

If its on online mode then you are load single image. And after that you are releasing it. But in off line mode you are storing the images in a dictionary. Try loading image one by one and after that try to release the image when not in use.

A better approach will be store the image path in dictionary, not the image. Then load the image from that path. Hope this helps.. :)

Edit:

From your comment mainData may be causing the problem. If online mode are you storing images to mainData? If no then I guess you've found the problem. As you said in online mode mainData is only holding the urls no the images thats why the memory do not goes up. Try to download and store the images in application document and then store the image path in mainData. Then just load images from application document. This will help to keep the memory low.

Community
  • 1
  • 1
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • NO. You assume wrong assumption. the dictionary holds only 1 image. see my comment up here . –  Jun 01 '14 at 11:06
  • Its like that : if i am in online mode, mainData holds only references to the images ,(urls) if its online mode he holds the actuall images. than every time, the dic gets the url/the image, according to the offline boolean. –  Jun 01 '14 at 11:09
  • ok thanks, this sounds nice. but loading each image from the NSFileManager in real time to the scroller, will perform good ? –  Jun 01 '14 at 11:16
  • 1
    Yeah, I used that without any problem. The app is performing nice. – Rashad Jun 01 '14 at 11:18