iOS - 获取目录中文件大小的总和 [英] iOS - Get sum of filesize in directory

查看:365
本文介绍了iOS - 获取目录中文件大小的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  NSURL * urlForPhoto = [FlickrFetcher urlForPhoto:self.photo格式: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];
}

我想将此限制为10MB,然后如果达到限制,缓存中最旧的照片,如何获取我保存的所有文件的总大小,并检查哪个文件是最旧的?

解决方案

你可以得到这样一个文件的大小

  NSError * attributesError = nil; 

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

int fileSize = [fileAttributes fileSize];

所以你可以遍历文件夹中的所有文件并添加文件大小...不知道如果这是一个直接的方式来获取目录大小,也是这个SO发表谈论这个aswell,你可以找到一个解决方案这里



要查找文件的创建日期,您可以执行

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

希望它有帮助


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?

解决方案

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

这篇关于iOS - 获取目录中文件大小的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆