0

私のアプリケーションでは、記録ファイルをドキュメントディレクトリに保存します。その後、これらすべてのファイルをドキュメントディレクトリから取得し、UITableviewがあるMymainClassにこれらすべてのファイルを表示します。このmainClassTableviewの任意の行をクリックすると、このファイルを再生する次のクラスに移動し、このファイルを削除して、Tableviewがあるお気に入りのレコーディングクラスにこのファイルを送信します。次に、再生ボタンアクションメソッドと削除ボタンアクションを実行します。メソッドは正常に機能しますが、このファイルをお気に入りのクラスTableviewに送信する方法がわかりません。ここに、基本的なアイデアを取得するための削除ボタンコードを表示します。

  -(IBAction)deleteFile
 {
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    NSString *documentPath = [docsDir stringByAppendingPathComponent:@"MyRecordings"];
    NSString *soundFilePath = [documentPath stringByAppendingPathComponent:fileToPlay];
    recordedTmpFile = [NSURL fileURLWithPath:soundFilePath];
    [[NSFileManager defaultManager] removeItemAtURL:recordedTmpFile error:nil];

}

[削除]ボタンのコードは、MainTableviewから選択した現在の記録ファイルを削除する方法を示しています。ユーザーが現在の記録ファイルを削除するのではなくお気に入りのクラスのテーブルビューに送信したい場合は、ここで別のドキュメントフォルダーを使用するかどうかを回答します。はいですか?次に、現在のファイル(recordedTmpFile)をこの新しいドキュメントフォルダーに保存する方法を説明します。

 -(IBAction)AddToFavourite
{
NSArray *dirPaths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir1 = [dirPaths1 objectAtIndex:0];
NSString *documentPath1 = [docsDir1 stringByAppendingPathComponent:@"MyFovouriteRecordings"];

// How to pass here the Current Recording File (recordedTmpFile) to this new document Folder.
}

NSlogをrecordedTmpFileにすると、結果が表示されます:file:// localhost / Users / Umar / Library / Application%20Support / iPhone%20Simulator / 4.2 / Applications / 9219677A-B0E3-4B78-B2E5-FEA49D689618 / Documents / MyRecordings / 06:Dec: 12_05:54:07%20PM + Active%20song

ヘルプが適用されます。ありがとう

4

2 に答える 2

1

元のファイルを使用してNSDataのインスタンスを作成するだけです。他の場所に書くより。

//write file to the other location
NSData *myData = [[NSData alloc] initWithContentsOfFile:originalPath];

[myData writeToFile:newDataPath options:NSDataWritingAtomic error:&error];

//delete file from original location
NSFileManager *fileManager = [[NSFileManager alloc] init];

[fileManager removeItemAtPath:originalPath error:&removeError];
于 2012-12-13T15:51:55.430 に答える
1
-(IBAction)AddToFavourite {
    NSArray *dirPaths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
    NSString *docsDir1 = [dirPaths1 objectAtIndex:0];
    NSString *documentPath1 = [docsDir1 stringByAppendingPathComponent:@"MyFovouriteRecordings"];
    NSString *soundFilePath1 = [documentPath1 stringByAppendingPathComponent:fileToPlay];
    // How to pass here the Current Recording File (recordedTmpFile) to this new document Folder.

    // First check if the directory existst
    if([[NSFileManager defaultManager] fileExistsAtPath:documentPath1]==NO) {
        NSLog(@"Creating content directory: %@",documentPath1);
        NSError *error=nil;
        if([[NSFileManager defaultManager] createDirectoryAtPath:documentPath1 withIntermediateDirectories:NO attributes:nil error:&error]==NO) {
            NSLog(@"There was an error in creating the directory: %@",error);   
        }
    }

    //then get the origin file path
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    NSString *documentPath = [docsDir stringByAppendingPathComponent:@"MyRecordings"];
    NSString *soundFilePath = [documentPath stringByAppendingPathComponent:fileToPlay];

    //Then convert paths to URL
    NSURL* originURL = [NSURL fileURLWithPath:soundFilePath];
    NSURL* destinationURL = [NSURL fileURLWithPath:soundFilePath1];

    //Then, you have to copy the file
    [[NSFileManager defaultManager] copyItemAtURL:destinationURL toURL:originURL error:NULL];
}

それでうまくいくはずです。エラーがあれば教えてください。

于 2012-12-14T16:21:58.910 に答える