ユーザーのアカウントごとに異なるフォルダを作成したいと考えています。ユーザーのフォルダに保存する必要があるユーザーの画像があります。どうすればわからないことを達成できますか。誰か助けてください。前もって感謝します。
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
現在、read-and-write-array-dictionary-and-other-collections-to-filesリンクをNSMutableArray
参照して保存しています。
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 に答える