我正在尝试将数据保存到NSFileManager创建的目录中,但是当我尝试检索数据和NSLog时,我得到null。此外,当您创建目录时,是否意味着您在指定的URL路径创建文件夹?继承人我正在使用的代码
NSError *error = nil;
NSFileManager *manager = [[NSFileManager alloc] init];
NSArray *urlsArray = [manager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSLog(@"%@", urlsArray);
// This will create a new url and append a photo title to the end
NSURL *url = [[urlsArray lastObject] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@", [self.recentPhoto objectForKey:FLICKR_PHOTO_TITLE]]];
NSLog(@"%@", url);
NSString *urlString = [NSString stringWithFormat:@"%@", [url absoluteString]];
NSLog(@"%@", urlString);
//create the directory
if(![manager fileExistsAtPath:urlString isDirectory:YES]){
BOOL success = [manager createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
NSLog(@"Error creating data path: %@", [error localizedDescription]);
}
}
//get url for a photo image and then store it.
NSURL *imageURL = [FlickrFetcher urlForPhoto:self.recentPhoto format:FlickrPhotoFormatLarge];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
[imageData writeToURL:url atomically:YES];
//get data to check if its stored
NSData *checkImageData = [[NSData alloc] initWithContentsOfURL:url];
//This returns null
NSLog(@"%@", checkImageData);
//this returns 0
NSLog(@"%d", [manager isReadableFileAtPath:urlString]);
答案 0 :(得分:3)
您的代码存在一些问题。
此:
NSURL *url = [[urlsArray lastObject] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@", [self.recentPhoto objectForKey:FLICKR_PHOTO_TITLE]]];
应该是:
NSURL *url = [[urlsArray lastObject] URLByAppendingPathComponent:[self.recentPhoto objectForKey:FLICKR_PHOTO_TITLE]];
此:
NSString *urlString = [NSString stringWithFormat:@"%@", [url absoluteString]];
应该是:
NSString *urlString = [url path];
此:
BOOL success = [manager createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:&error];
应该是:
BOOL success = [manager createDirectoryAtURL:[url URLByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error];
此:
[imageData writeToURL:url atomically:YES];
应该是:
NSError *error = nil;
BOOL res = [imageData writeToURL:url options:NSDataWritingAtomic error:&error];
if (!res) {
NSLog(@"Unable to write to %@: %@", url, error);
}
主要问题是使用absoluteString
代替path
将URL转换为文件路径。辅助节点在创建目录时传递文件名而不是路径。不必要地使用stringWithFormat:
并没有引起任何问题,但请立即打破这种习惯。实际需要格式化字符串时,只能使用stringWithFormat:
。不需要将字符串分配给字符串变量。
答案 1 :(得分:0)
您没有为目录中的图像指定文件名,这可以通过在创建目录后立即添加此行来解决:
// Right after the directory is created (I suppose the format is png) : url = [url URLByAppendingPathComponent:@"image.png"]; // Now you can continue with the rest of your code : //get url for a photo image and then store it. NSURL *imageURL = [FlickrFetcher urlForPhoto:self.recentPhoto format:FlickrPhotoFormatLarge]; NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL]; [imageData writeToURL:url atomically:YES]; //get data to check if its stored NSData *checkImageData = [[NSData alloc] initWithContentsOfURL:url]; //This returns null NSLog(@"%@", checkImageData); //this returns 0 NSLog(@"%d", [manager isReadableFileAtPath:urlString]);
答案 2 :(得分:0)
我从未创建过文件管理器的实例。我总是简单地使用默认值 看看这是否有帮助:
NSFileManager *manager = [NSFileManager defaultManager];