NSdata writeToURL不工作
我正在尝试将NsData文件保存到目录中。这是我的方法代码:NSdata writeToURL不工作,nsdata,writetofile,cs193p,Nsdata,Writetofile,Cs193p,我正在尝试将NsData文件保存到目录中。这是我的方法代码: - (void)cacheImage:(NSData *)imageData withTitle:(NSString *)title { NSURL *cacheURL = (NSURL *)[[self.fileMenager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] objectAtIndex:0]; //we take the URL
- (void)cacheImage:(NSData *)imageData withTitle:(NSString *)title
{
NSURL *cacheURL = (NSURL *)[[self.fileMenager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] objectAtIndex:0]; //we take the URL of the cache Directory and comvert the url of the cache directory into a string
NSURL *imageFolder = [cacheURL URLByAppendingPathComponent:PHOTO_CACHE_FOLDER];
if ([self.fileMenager isWritableFileAtPath:[cacheURL path]]) { //check if the cache directory is writable
if (![self.fileMenager createDirectoryAtURL:cacheURL withIntermediateDirectories:NO attributes:nil error:nil]) { //check if the directory of our image is already exist
NSURL * fileToWrite = [[imageFolder URLByAppendingPathComponent:title isDirectory:NO] URLByAppendingPathExtension:@"jpg"]; //take the complete url of image
if ([imageData writeToURL:fileToWrite atomically:YES]) //atomically = safe write
{
NSArray *debug = [self.fileMenager contentsOfDirectoryAtPath:[imageFolder path] error:nil];
for (NSURL *url in debug)
NSLog(@"url prooooo : %@",url);
}
}
}
}
这是url(fileToWrite)的一个示例,我尝试在其中编写:
file://localhost/Users/MarcoMignano/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/93B0A0AC-EC13-4050-88CA-F46FBA11001E/Library/Caches/image_cache/Mozart%201.jpg
要点很简单,writeToURL方法返回NO,我不明白为什么,url看起来是正确的
有人可以帮我的忙
多谢各位 主要问题是您创建了错误的目录。您需要创建
imageFolder
目录
if ([self.fileManager createDirectoryAtURL:imageFolder withIntermediateDirectories:YES attributes:nil error:nil]) {
请注意,您不需要
代码>在if
语句中。您是否调试并查看它能走多远?imageData
non-nil?如果createDirectoryAtURL
返回否,请使用error
参数并记录结果错误。现在一切正常,但我必须删除应用程序并重新启动它,否则createDirectoryAtURL返回否谢谢