我使用以下代码将从Flickr下载的照片缓存到设备的内存中:
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];
}我想将其限制为10MB,然后如果达到了删除缓存中最旧照片的限制,我如何才能获得我保存的所有文件的总大小,并检查哪个文件是最旧的?
发布于 2012-02-17 00:34:11
你可以像这样得到一个文件的大小
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
int fileSize = [fileAttributes fileSize];你可以遍历文件夹中的所有文件,然后把文件sizes...not加起来,确定有没有直接的方法来获得目录的大小,还有这篇文章也在讨论这个,你可以找到一个解决方案here
要查找文件的创建日期,您可以执行以下操作
NSString *path = @"";
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs valueForKey:NSFileCreationDate]; //or NSFileModificationDate希望能有所帮助
https://stackoverflow.com/questions/9314954
复制相似问题