【问题标题】:Creating multiple directories in documents folder for iOS在 iOS 的文档文件夹中创建多个目录
【发布时间】:2013-08-06 01:36:21
【问题描述】:

我想创建多个目录,我不知道怎么做。这是我到目前为止的代码。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"tops",@"bottoms",@"right" ];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder

}

我想要有 4 个名为 tops bottoms right 和 left 的目录,但是如果我以上述方式执行此操作,它将不起作用。有没有办法使用此代码创建多个目录?还是我的代码错了?谢谢!

【问题讨论】:

  • 需要迭代四次才能创建4个目录
  • 嗯 .. this exact question 不久前被另一个用户询问(被否决并搁置)。您的个人资料是在几分钟前创建的!我很怀疑。
  • @Amar 你是对的。它看起来像一个。
  • 这还是个有用的问题

标签: iphone ios directory nsdocumentdirectory


【解决方案1】:

试试这个代码。

按照您的要求创建目录。

NSArray *directoryNames = [NSArray arrayWithObjects:@"tops",@"bottoms",@"right",@"left",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

for (int i = 0; i < [directoryNames count] ; i++) {
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder
}

在应用中的任何位置使用您创建的目录。

NSArray *directoryNames = [NSArray arrayWithObjects:@"tops",@"bottoms",@"right",@"left",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

// This will return the folder name in the 0 position. In yours "tops"
NSString *topDirPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:0]];
// This will return the file path in your "tops" folder
NSString *filePath = [topDirPath stringByAppendingPathComponent:@"MYIMAGE"];

存储图像文件

NSData *imageDataToStore = UIImagePNGRepresentation(image);
[imageDataToStore writeToFile:filePath atomically:YES];

检索图像文件

// Convert the file into data and then into image.
NSData *imageData = [[NSData alloc] initWithContentsOfFile:filePath];
UIImage *yourImage =  [UIImage imageWithData:imageData];

【讨论】:

  • 我收到错误说使用未声明的标识符“i”以及我应该如何调用目录以便我可以将图片保存在..
  • 对不起,我在 SO 的答案框中写了代码。如此而已。现在我在for 循环中声明了i。立即检查。
  • 最后再问一个问题,您如何回忆目录,以便我可以将照片保存在我创建的目录中..
  • @AppleDevram 我正在为您编写代码。你只是在玩我的答案中添加/删除勾选按钮。
  • @AppleDevram 我仍在为您编写代码。如果你想完成你的任务,然后等待。如果您想快速回答,请再问一个问题以召回文件夹。
猜你喜欢
  • 2012-03-01
  • 2017-08-31
  • 2014-09-21
  • 2020-11-18
  • 2022-01-15
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多