7

I have the following code I use to cache photos I load off Flickr in the device's memory:

NSURL *urlForPhoto = [FlickrFetcher urlForPhoto:self.photo format:FlickrPhotoFormatLarge];
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [rootPath stringByAppendingString:[self.photo objectForKey:FLICKR_PHOTO_ID]];
NSData *dataForPhoto;
NSError *error = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
    dataForPhoto = [NSData dataWithContentsOfFile:imagePath];
} else {
    dataForPhoto = [NSData dataWithContentsOfURL:urlForPhoto];
    [dataForPhoto writeToFile:imagePath atomically:YES];
}

I want to limit this to 10MB and then if the limit is reached to remove the oldest photo in the cache, how can I get the total size of all the files I've saved and check which one is the oldest?

8vius
  • 5,786
  • 14
  • 74
  • 136
  • Yep, I have to make the comment longer so I can answer. – 8vius Apr 24 '12 at 13:48
  • Calculating the size that a directory takes up on disk is actually a little more involved. Find out how to in [this answer](http://stackoverflow.com/a/28660040/104790) to a similar question. – Nikolai Ruhe Feb 23 '15 at 08:43

1 Answers1

10

You can get the size of a file like so

NSError *attributesError = nil;

    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

    int fileSize = [fileAttributes fileSize];

So you can maybe iterate through all the files in the folder and add up the file sizes...not sure if theres a direct way to get the directory size, also theres this SO post talking about this aswell, you can find a solution here

To find the creation date of the file you can do

NSString *path = @"";
    NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
    NSDate *result = [fileAttribs valueForKey:NSFileCreationDate]; //or NSFileModificationDate

Hope it helps

Community
  • 1
  • 1
Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Awesome Daniel, one more thing, how would be a sure way of checking if a directory exists? Because I'm gonna create a subdirectory in the documents directory if it doesn't exist – 8vius Feb 16 '12 at 16:41
  • [NSFileManager defaultManager] itemExistsAtPath should do the trick – Daniel Feb 16 '12 at 16:43
  • 3
    It's actually fileExistsAtPath, Daniel. Now, can you answer one more thing? If I get the attributes and get the NSFileSize object on it, I get a small number, if I call it on a file I get a large number (both in bytes I'm assuming), doesn't getting the file size for the directory give me the total of all files within it? – 8vius Feb 16 '12 at 19:24
  • 1
    @8vius not sure as i have never tried that, but by your comment i guess not? – Daniel Feb 16 '12 at 19:56
  • 1
    It doesn't seem like it, so to be sure I'm just adding all the files, it's odd though, if you query for the size of a directory, its size should be the total size of its contents – 8vius Feb 16 '12 at 19:57