2

I have created an application , where i ll be downloading the images from server and storing it locally on the iPhone's file system. Its happening fine. Now the problem is , i want to clear the locally cached images on iPhone when ever i quit my application.

How do i delete those cached images on iPhone. it's using Secondary memory on iPhone, how do i access it programmatically ?

Thank You in advance. Suse.

suse
  • 10,503
  • 23
  • 79
  • 113
  • "Secondary memory"? You'll have to use the same terms as everyone else if we're going to understand your question. The iPhone has no such thing as "Secondary memory". What is it that you mean? – occulus Mar 23 '11 at 09:33
  • What do you mean by Secondary memory? How do you store downloaded images? – zrzka Mar 23 '11 at 09:39
  • I meant as permanent storage on iPhone. Its where the files get stored on iPhone. I'm using NSPathUtilities library to achieve this. – suse Mar 23 '11 at 09:45
  • Ah got you, ok, you should update your question with "permanent storage" or "file system". – occulus Mar 23 '11 at 11:38
  • Also keep in mind that you can only store images up to 2GB (document directory limit). So you should also think of cleaning the temporary stored files when you exceed the limit, not only when the app terminates. – ThE uSeFuL Aug 01 '11 at 06:11

2 Answers2

4

Clean them up in your applicationWillTerminate method.

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *imagesFiles = [fileManager contentsOfDirectoryAtPath:saveDirectory error:&error];
    for (NSString *file in imagesFiles) {
        error = nil;
        [fileManager removeItemAtPath:[saveDirectory stringByAppendingPathComponent:file] error:&error];
        /* do error handling here */
    }
}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
Kirby Todd
  • 11,254
  • 3
  • 32
  • 60
  • 2
    I would sugest runnig this in block via GCD. – rckoenes Mar 23 '11 at 10:13
  • 1
    Using applicationWillTerminate isn't a very good strategy on iOS4+ with multitasking. This method often won't get called -- an app will be background, and then can be dumped from memory if other apps are run, without applicationWillTerminate getting called. See the flowcharts at http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/ – occulus Mar 23 '11 at 11:44
4

My answer would be as per Kirky Todds, but don't use applicationWillTerminate -- it's not a good strategy on iOS4+ with multitasking, because this method often won't get called -- an app will be background, and then can be dumped from memory if other apps are run (or the phone is turned off/restarted), without applicationWillTerminate getting called. (OR it will be foregrounded again. Either way, your cache doesn't get cleared.)

Instead, consider either clearing at startup (applicationDidFinishLaunching), or in applicationWillEnterForeground. The latter will tend to get called more frequently, because the former is only called on an actual proper app lauch (and not on a resume from being backgrounded).

For more info on backgrounding and the events generated, see:

http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/

occulus
  • 16,959
  • 6
  • 53
  • 76