gpt4 book ai didi

objective-c - copyItemAtPath、moveItemAtPath 和 createFileAtPath 似乎都创建了一个不可读的文件

转载 作者:行者123 更新时间:2023-11-29 04:41:25 25 4
gpt4 key购买 nike

所以我试图找出为什么我无法制作可读的文件副本。我尝试过 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/933becb027 83c8f9c715acbdca0e15ed.caf

有人对我做错了什么有什么建议吗?谢谢

最佳答案

将最后一行更改为:

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

因为带有 NSURL 的路径 url 需要以 file://开头。

关于objective-c - copyItemAtPath、moveItemAtPath 和 createFileAtPath 似乎都创建了一个不可读的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10339923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com