所以我想弄清楚为什么我不能复制我的文件是可读的。我试过了moveItemAtPath,copyItemAtPath和createFileAtPath。每次从原始路径读取数据都可以正常工作,但是从新文件路径读取数据会产生0字节的nil NSdata对象。
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/mobile/Applications/19531C7F-F408-45B9-B417-09315BB15B49/Documents/933becb02783c8f9c715acbdca0e15ed.caf < / p>
有没有人对我做错了什么有任何建议?感谢
答案 0 :(得分:1)
将您的最后一行更改为:
SData* audioAfter = [NSData dataWithContentsOfFile:newAudioFile]; // NOT nil
因为带有NSURL
的路径网址需要一个文件://开头。