2

I have created an app in which users will be able to upload the images and they can also see all the images uploaded by other users. I have integrated the code for the pagination and downloading the 10 images at a time and showing them on the UITableView. I have used AFNetworking for this task which is saving the image in the cache memory. The problem is that when user keeps downloading the images and count goes to around 300 images, app crashes because the device runs out of memory. I am looking for the best solution of this issue. What I have thought is to keep 50 images in the cache at a time and when user downloads the newer images, older ones will be deleted from the cache. Please also let me know if I can do this with AFNetworking.

Developer
  • 6,375
  • 12
  • 58
  • 92
  • are You storing the images in document directory? post your code where the leak is.. – Ganapathy Nov 11 '13 at 10:22
  • @Ganapathy No, AFNetworking stores the images in its cache and I don't know where this cache memory is. And this is the issue of memory allocation not of the leaks. – Developer Dec 18 '13 at 07:19
  • Clear the cache memory once you going to start download the images each time. e.x if you are going to download 2nd 50 images, just clear the cache and start download. Like that try once. – Ganapathy Dec 18 '13 at 08:37
  • @Ganapathy Can you tell me how can I clear the cache? – Developer Dec 18 '13 at 09:49
  • http://stackoverflow.com/questions/11070175/how-to-cancel-network-request-with-afnetworking – NANNAV Dec 19 '13 at 06:43
  • 1
    you may use SDWebImage instead, its working fine for more than 300 images. – Aklesh Rathaur Dec 21 '13 at 06:58
  • http://stackoverflow.com/questions/17644197/how-to-clear-image-cache-or-any-cache-in-afnetworking – Rajarshi Goswami Dec 24 '13 at 06:36
  • In my case, the number of images will be unlimited as it is a drawing app and the number of images can be unlimited so I want a way to delete the cache. – Developer Dec 26 '13 at 08:11

3 Answers3

2

You should use SDWebImage instead of downloading images it will cache and you can customise that caching option also .

Iam using this SDWebImage for caching 150 +images in UICollectionview and same in UITableview and its work perfect.

Initially we need to add a placeholder then it will appear one by one.

implementation

1.Take out the SDWebImage code SDWebImage

2.Import the header file in to your viewcontroller

#import <SDWebImage/UIImageView+WebCache.h>

3.then add a single line of code inside cellforrowindex() method. i will gve you example code snaps

 // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
Bangalore
  • 1,572
  • 4
  • 20
  • 50
1

Try APSmartStorage. You can manage number of images stored in memory. And if 'memory warning' occurred images will be removed from memory. But anyway it's ok because all images stored in files, so you don't need to reload them from network.

0

As @Aklesh Rathaur mentioned in comments, you could try SDWebImage, which is also used by Facebook on their mobile app.

It does a pretty good job at caching and freeing memory when more memory is required. Quoting How is SDWebImage better than X?

On the other side, SDWebImage caches the UIImage representation in memory and store the original compressed (but decoded) image file on disk. UIImage are stored as-is in memory using NSCache, so no copy is involved, and memory is freed as soon as your app or the system needs it.

Let us know your results :-)

Community
  • 1
  • 1
jweyrich
  • 31,198
  • 5
  • 66
  • 97