簡體   English   中英

是否可以將文件保存在NSDocumentDirectory內的自定義文件夾中

[英]Is it possible to save a file in a custom folder inside NSDocumentDirectory


是否可以在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);

創建該文件夾沒有任何問題,但是音頻文件未保存在該文件夾中。 謝謝。

對的,這是可能的。 您只需要在文檔目錄中創建一個文件夾。

- (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];
}

然后,每當您要存儲某些內容並將其存儲到文件夾中時,都要提取該文件夾。

是的,有可能,您可以將目錄名稱附加在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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM