2013-08-05 155 views
3

我想制作多个目录,但我不知道如何。这是我的代码,直到现在。在iOS的文档文件夹中创建多个目录

- (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个目录,命名为顶部底部右侧和左侧但是如果我做它用上述方法不工作..有没有办法使用这个代码,以使多个目录?或者是我的代码错了?谢谢!

+0

需要迭代四次创造4个目录 –

+0

嗯.. [这个确切的问题( http://stackoverflow.com/questions/18055646/creating-multiple-directory-in-documents)前一段时间被另一个用户问过(被低估并将**搁置**)。而你的个人资料是在几分钟前创建的!我很怀疑。 – Amar

+0

@Amar你说得对。它看起来像是一样的。 –

回答

8

试试看看这个代码。

按照您的要求创建目录。

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

我得到的错误说使用未声明的标识符“我”,我应该如何回忆该目录,以便我可以保存图片.. – AppleDevram

+0

对不起,我已经写在SO代码框中的代码。只有这样。现在我已经在'for'循环中声明了'i'。现在检查。 –

+0

最后还有一个问题,您如何回忆目录,以便我可以将照片保存在我创建的目录中。 – AppleDevram

相关问题