0

在我的应用程序中,我将记录文件保存在文档目录中。然后稍后从文档目录中获取所有这些文件,并在我的 mainClass 中显示所有这些文件,其中我有 UITableview。当我单击此 mainClass Tableview 的任何行时,它会转到下一个类,我在其中播放此文件,删除此文件并将此文件发送到我最喜欢的录音类,在那里我有 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 中选择的当前录制文件。现在如果用户想要将当前录制文件发送到我最喜欢的类 Tableview 而不是删除它那么我是否在此处使用另一个文档文件夹如果回答是是的?那么我们如何将当前文件(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 记录的TmpFile 显示结果: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+活动%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 回答