存储大于系统

时间:2019-02-13 08:24:09

标签: ios objective-c storage

我想模拟存储空间不足的情况,所以我尝试使用存储空间已满的文件来复制文件,但是我发现它永远都无法达到目的,而且我发现占用的存储空间大于系统空间,这怎么办? 代码如下:

+ (void)copeFiles
{
    NSString *srcPath = [self cloudStorageCacheFolderPath];
    NSLog(@"copy: src path:%@ ,size:%f", srcPath, [self sizeOfcloudStorageCacheFolder]);
    int base = 300;
    int i = base;
    while (1) {
        i++;
        if (i > base + 100) {
            break;
        }
        NSInteger freeSizeM = [self freeFileStorageSize];
        if (freeSizeM > 1024) {
            NSString *newFilePath = [NSString stringWithFormat:@"%@-%d", srcPath, i];
            [self copyFolderContentFromSrc:srcPath toDestPath:newFilePath];
            NSLog(@"copy: i:%d, freeSizeM:%li", i, (long)freeSizeM);
        }else {
            break;
        }
    }
}
+ (void)copyFolderContentFromSrc:(NSString *)srcPath toDestPath:(NSString *)dstPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSLog(@"copy: src:%@ dst:%@", srcPath, dstPath);
    BOOL isDir;
    if (![fileManager fileExistsAtPath:dstPath isDirectory:&isDir] || !isDir) {
        BOOL sucess = [fileManager createDirectoryAtPath:dstPath withIntermediateDirectories:YES attributes:nil error:nil];
        NSLog(@"copy: isDir:%d sucess:%d", isDir, sucess);
    }
    NSArray* array = [fileManager contentsOfDirectoryAtPath:srcPath error:nil];
    int i = 0;
    for (NSString *fileName in array) {
        i++;
        NSString *srcFullPath = [srcPath stringByAppendingPathComponent:fileName];
        NSString *toFullPath = [dstPath stringByAppendingPathComponent:fileName];
        NSError *error;
        BOOL isSucess = [fileManager copyItemAtPath:srcFullPath toPath:toFullPath error:&error];
        NSLog(@"copy:%d", i);
        if (!isSucess) {
            NSLog(@"copy:error:%@", error);
        }
    }
}

image1 image2 这些图像可能是看不见的,所以我只简单地描述这种现象。我的iPhone总容量为32GB,但在设置中显示的可用存储空间为93GB。

1 个答案:

答案 0 :(得分:0)

是的,如果再次运行它,它也会增加:)

其背后的原因是它将仅存储引用而不是实际文件。并且我在Mac系统中做了同样的操作以填充模拟器的空间,最后得到了同样的256GB系统,显示了4.3 TB的空间

  • 在Device中,您可以使用录制视频来填充空间,但是仍然无法得到该错误,因为Apple会以这种方式进行管理。

1)删除所有应用程序目录中存储的所有临时文件。

2)然后清除缓存。

苹果公司将很好地管理存储空间管理,对此仍有一些神秘之处。