【问题标题】:Is it possible to save a file in a custom folder inside NSDocumentDirectory是否可以将文件保存在 NSDocumentDirectory 内的自定义文件夹中
【发布时间】:2015-01-12 15:48:57
【问题描述】:


是否可以在NSDocumentDirectory 的自定义文件夹中保存文件(任何类型)?我的意思是每次我尝试保存时都将其用作资源文件路径:

NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], fileName, nil];
outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

NSString *imageDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

因此它总是保存在以下目录中,
~/Library/Application Support/iPhone Simulator/......./Documents/file.ext

但是是否可以将该文件保存在这样的自定义文件夹中:
~/Library/Application Support/iPhone Simulator/......./Documents/CustomFolder/file.ext

如果是,您能告诉我有关过程的详细信息吗?
提前致谢。
祝你有美好的一天。

补充:

这里,其实我保存了一个音频录音文件。该文件的名称来自当前时间。我想将该录音文件保存在自定义文件夹中。

这是我的代码:

- (NSString *) dateString
{
    // return a formatted string for a file name
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"ddMMMYYY_hh:mm:ssa";
    return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".m4a"];
}

// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], fileName, nil];
outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

outputFileURL我保存我录制的声音。

我试试这个:

// Set the audio file
NSString *customDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
customDirectoryPath = [customDirectoryPath stringByAppendingPathComponent:@"MyCustomDirectory"];

NSArray *pathComponents = [NSArray arrayWithObjects:customDirectoryPath, fileName, nil];
    NSLog(@"pathComponents %@", pathComponents);
    outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
    NSLog(@"outputFileURL %@", outputFileURL);

文件夹创建没有任何问题,但音频文件未保存在该文件夹中。 谢谢。

【问题讨论】:

  • 在控制台上打印时的 outputFileURL 是什么?检查它是否是正确的文件路径
  • @Puran NSLog 显示有一个新创建的文件夹。但实际上它并没有被创造出来。因为stringByAppendingPathComponent不见了,NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];是Rameswar先生提供的神奇台词。多谢兄弟。 :)

标签: ios objective-c nsurl nsdocumentdirectory


【解决方案1】:

是的,这是可能的。您只需要在文档目录中创建一个文件夹。

- (NSURL *)audioRecordingPath:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"/Recorded"];
    NSFileManager *fileManager  = [NSFileManager defaultManager];

    NSError *error = nil;
    if (![fileManager fileExistsAtPath:folderPath])
        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

    NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];

    return [NSURL fileURLWithPath:filePath];
}

然后每次你想存储东西的时候获取文件夹并存储到里面。

【讨论】:

  • 非常感谢您的评论和抱歉迟到的回复。它绝对有效。但是我真正想要的东西是在我的旧帖子下面添加的。你能看一下吗?谢谢。
  • @Tulon 感谢您告诉我。我在您编辑后看到了您的问题,这是我正在使用的代码。我只是将一个字符串传递给这个方法,它会在我的自定义文件夹中创建一个文件。如果您仍然遇到任何问题或需要任何帮助,请告诉我。
  • 锐利、快速、快速。这是工作。谢谢你的努力兄弟。保重并保持健康。 :)
  • 欢迎您@Tulon。如果无论如何您知道哪些开发人员可以像堆栈溢出中的 facebook 朋友一样保持联系,那么请分享。如果我们认识一些开发人员以便可以互相帮助,那将会很有帮助。顺便说一句,虽然我很久以前就开始记账了,但对这个地方还是很陌生。我很乐意提供帮助。
  • 当然。但你不能在这里与他们保持联系。至少不像 facebook 或其他社交网络。您可以通过不同的聊天部分来敲他们。但如果你愿意,你必须亲自将它们添加到其他盘子上。如果你愿意,可以加我。 :)
【解决方案2】:

是的,这是可能的,您可以在 NSDocumentDirectory 之后附加目录名称,然后可以执行类似的操作

 NSString *customDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 customDirectoryPath = [customDirectoryPath stringByAppendingPathComponent:@"MyCustomDirectory"]
 if (![[NSFileManager defaultManager] fileExistsAtPath:customDirectoryPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:customDirectoryPath
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:&error];

【讨论】:

  • 看起来我和 Rameswar 同时回答了,但都是正确答案!
  • 天哪,我不知道该给谁投票!两个都对,同时提交!!!
  • @Puran 非常感谢您的回答。这是 100% 正确的。如果可能的话,你可以看看我的主要帖子的“附加”部分。再次感谢。
  • 是的,同时!如果我能同时接受他们两个,我会很高兴。 :( 但是,如果我将答案设置为“OLD”,那么 Rameswar 先生的答案首先出现。@Acey
  • @Puran 是的,我们都同时回答了,而且确实都是正确的答案。干杯普兰。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
相关资源
最近更新 更多