如何从一个文档文件夹到另一个文档文件夹获取文件?

时间:2012-12-13 13:53:28

标签: iphone xcode uitableview document

在我的应用程序中,我在文档目录中保存录制文件。之后,从文档目录中获取所有这些文件,并在My mainClass中显示所有这些文件,其中我有UITableview。 当我点击这个mainClass Tableview的任何一行时,它会转到下一个我播放此文件的类,删除此文件并将此文件发送到我最喜欢的录制类,其中我有Tableview for it.Now我的播放按钮操作方法和删除按钮操作方法工作正常,但我不知道如何将此文件发送到我的Favurite类Tableview.now我在这里显示我的删除按钮代码,通过它我们可以得到基本的想法。

  -(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)保存在这个新的Document文件夹中。

 -(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录制文件时,它显示结果:file://localhost/Users/Umar/Library/Application%20Support/iPhone%20Simulator/4.2/Applications/9219677A-B0E3-4B78-B2E5-FEA49D689618/Documents/MyRecordings/06 :12月:12_05:54:07%20PM +有源%20song

任何帮助都将被包含。谢谢

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];
}

应该这样做,告诉我是否有任何错误。

相关问题