Iphone 在iOS的documents文件夹中创建多个目录

Iphone 在iOS的documents文件夹中创建多个目录,iphone,ios,directory,nsdocumentdirectory,Iphone,Ios,Directory,Nsdocumentdirectory,我想使多个目录,我不知道如何。这是到目前为止我的密码 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return sel

我想使多个目录,我不知道如何。这是到目前为止我的密码

- (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个目录,命名为顶部底部的权利和左侧,但它不工作,如果我这样做上面的方式。。有没有一种方法可以使用此代码创建多个目录?还是我的代码错了?谢谢

试试这段代码

按要求创建目录

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
}
存储图像文件的步骤

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];
检索图像文件的步骤

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];

您需要迭代四次才能创建4个directoriesHmm。。不久前被另一位用户询问(被否决并搁置)。您的个人资料是在几分钟前创建的!我很怀疑。@Amar你是对的。它看起来是一样的。这仍然是一个有用的问题。我遇到了一些错误,比如使用了未声明的标识符“i”,以及我应该如何调用目录以便将图片保存在中..很抱歉,我在so的回答框中编写了代码。仅此而已。现在我在
for
循环中声明了
I
。现在检查一下。最后还有一个问题,您如何回忆目录,以便我可以将照片保存在我创建的目录中。@AppleDevram我正在为您编写代码。您只是在玩添加/删除我答案中的勾号按钮。@AppleDevram我仍然在为您编写代码。如果您想完成任务,请等待。如果你想快速回答,那么问另一个问题来回忆文件夹。