参考サイト
[XCODE] NSFileManagerを用いてディレクトリを作成する、ファイルを保存する – YoheiM .NET
NSFileManagerの「createDirectoryAtPath:withIntermediateDireotries:attributes:error:」を用いることで、 ディレクトリを作成できます。だそうです。
参考サイトではcashフォルダに新規フォルダを作ってますがここではDocumentsフォルダに新規フォルダを作って画像ファイルを保存します。
-(void)sharedFileSave:(UIImage *)sender { NSLog(@"しぇあどふぁいるせーぶ"); //保存する画像をPNGでnsdataに変換? UIImage *image = sender; NSData *data = UIImagePNGRepresentation(image); //ドキュメントフォルダ(共有フォルダ)のパスの取得 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *DocumentsDirPath = [paths lastObject];//これがドキュメントフォルダ(共有フォルダ)のパスになるらしい。 // 続いて、新規で作るディレクトリの絶対パスを作成します。 NSString *newDocumentsDirPath = [DocumentsDirPath stringByAppendingPathComponent:@"sampleDirectory"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error = nil; BOOL created = [fileManager createDirectoryAtPath:newDocumentsDirPath withIntermediateDirectories:YES attributes:nil error:&error]; // 作成に失敗した場合は、原因をログに出します。 if (!created) { NSLog(@"failed to create directory. reason is %@ - %@", error, error.userInfo); } //保存する名前を決める。 NSString *exportFilename = @"sample2.png"; //保存先のパスと名前を合体させたパスを作成。 NSString *exportFilePath = [newDocumentsDirPath stringByAppendingPathComponent:exportFilename]; ; if ([data writeToFile: exportFilePath atomically:YES]) { NSLog(@"ほぞんOK"); } else { NSLog(@"ほぞんError"); } }
これを実行してiTunesでアプリの共有フォルダを見るとsampleDirectoryフォルダが出来ており、中にsample2.pngファイルが入っています。