1

我想为每个用户的帐户创建不同的文件夹。有些用户的图像需要存储在他们的文件夹中。我怎么能做到这一点,我不知道。谁能帮助我。提前致谢。

4

3 回答 3

0

您可以通过以下方式在您的应用程序中创建目录:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *newFolder = [(NSString*)[paths objectAtIndex:0] stringByAppendingPathComponent:@"uniqueIdentifierForAccount"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:newFolder]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:newFolder withIntermediateDirectories:YES attributes:nil error:nil];
    }
于 2012-10-30T04:55:30.870 回答
0

Create directory在你的应用程序中是这样的:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"UniqueUserAccount"];

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

现在保存NSMutableArray参考read-and-write-array-dictionary-and-other-collections-to-files链接。

folderPath = [folderPath stringByAppendingPathComponent:@"array.out"]; 
[yourArray writeToFile:folderPath atomically:YES];

像这样读取数组:

NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:folderPath];
NSLog(@"%@",arrayFromFile);

编辑:从图像数组中将图像保存在用户帐户文件夹中

for(int i = 0;i < [arrImages count];i++)
{
  folderPath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d",i]]; //here folder Path with Image file
  //Our goal is to create UIImage into NSData if u have image in bundle then take like this:
  NSMutableString *imgPath;
  imgPath=[[NSMutableString  alloc] initWithString: [[NSBundle mainBundle] resourcePath]]; //get bundle path
 [imgPath stringByAppendingPathComponent:[arrImages objectAtIndex:i]]; //get image from bundle to write
 NSData *imageData = [NSData dataWithContentsOfFile:imgPath]; //create nsdata of image

 //if u have UIImage 
 NSData *imageData = UIImagePNGRepresentation([arrImages objectAtIndex:i]) //image reference here to convert into data

 [imageData writeToFile:folderPath atomically:YES]; //write here
}
于 2012-10-30T05:05:28.490 回答
0

在我看来,您实际上只需要存储一个连接到名称的数组,而不需要实际的文件夹。我建议使用 NSUserDefaults。您可以像这样实现此功能:

    //instantiate the NSUserDefaults
    NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];

    NSArray *userArray;
    NSString *username;

    //Then to store:
    [storage setObject:userArray forKey:username];

    //and to recall:
    userArray = [storage objectForKey:username];
于 2012-10-30T06:00:44.673 回答