2012-04-26 83 views
0

所以我想弄清楚为什么我无法制作可读的文件副本。我试过moveItemAtPath,copyItemAtPath和createFileAtPath。每次从原始路径读取数据都可以正常工作,但是从新文件路径中读取数据会导致0字节无NSdata对象。copyItemAtPath,moveItemAtPath和createFileAtPath都似乎创建了一个无法读取的文件

NSData* tempData = [NSData dataWithContentsOfURL:tempSoundFile]; // reads just fine 

NSError* error; 
NSFileManager* fileMgr = [NSFileManager defaultManager]; 
NSString* docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSString* uniqueFileName = [[NSString stringWithFormat:@"%@-%@", description.text, [NSDate date]] MD5]; 
NSString* newAudioFile = [NSString stringWithFormat:@"%@/%@.caf", docDir, uniqueFileName]; 

if (LOG) { NSLog(@"Copying %@ to %@", tempSoundFile.path, newAudioFile); } 

// Have tried either or both as well as moveItemAtPath with the same result 
[fileMgr createFileAtPath:newAudioFile contents:tempData attributes:nil]; 
//[fileMgr copyItemAtPath:tempSoundFile.path toPath:newAudioFile error:&error]; 

NSData* audioAfter = [NSData dataWithContentsOfURL:[NSURL URLWithString:newAudioFile]]; // nil data 

日志输出:

复制/var/mobile/Applications/19531C7F-F408-45B9-B417-09315BB15B49/Documents/8554temp.caf到/ var /移动/应用/ 19531C7F-F408-45B9 -B417-09315BB15B49/Documents/933becb02783c8f9c715acbdca0e15ed.caf

有没有人有任何建议,我做什么错了?由于

+1

假设你已经检查过各种文件管理器方法的返回值并且它们表示成功(返回YES)是否安全?你是否还证实'[NSURL URLWithString:newAudioFile]'给出了预期的结果? – 2012-04-26 19:29:39

+0

是'audioAfter' nil,还是长度为0的有效NSData实例? – Caleb 2012-04-26 19:52:02

回答

1

更改你的最后一行:

SData* audioAfter = [NSData dataWithContentsOfFile:newAudioFile]; // NOT nil 

因为与NSURL路径URL期待文件://开头。

+0

谢谢!这是完全正确的。 URL不是自动生成文件://像我想的那样。 – valheru 2012-04-26 20:56:58

相关问题