无法在缓存目录中创建文件

时间:2013-09-29 18:01:11

标签: ios nsfilemanager

我正在尝试设置一个缓存目录,以便在我的应用中使用,但是由于我不知道的原因而没有创建这些文件。我究竟做错了什么?以下是我正在使用的方法:

在课程实用程序中:

+(NSString *)imageCachePath {
   NSString *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   NSString *pieceImagesDirectory = [cacheDirectory stringByAppendingPathComponent:@"PieceImages"];
   NSLog(@"imageCachPath is %@",pieceImagesDirectory);
   return pieceImagesDirectory;
}
+ (void)cacheImage:(UIImage *)image usingName:(NSString *)name;
{
   NSLog(@"Caching image %@",name);
   NSString *pieceImagesDirectory = [self imageCachePath];
   BOOL isDir = NO;
   NSError *error;
   if (! [[NSFileManager defaultManager] fileExistsAtPath:pieceImagesDirectory isDirectory:&isDir] && isDir == NO) {
      [[NSFileManager defaultManager]createDirectoryAtPath:pieceImagesDirectory withIntermediateDirectories:YES attributes:nil error:&error];
      NSLog(@"Error after creating directory:\n%@",error);
   } else {
      // file exists - I don't expect to use the else block. This is for figuring out what's going on.
      NSLog(@"File %@ exists -- is it a directory? %@",pieceImagesDirectory, isDir?@"YES":@"NO");
   }
   NSString *nameToUseInFilename = [name stringByReplacingOccurrencesOfString:@" " withString:@"_"];
   NSString *fullPath = [pieceImagesDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",nameToUseInFilename]];
   NSData *data = UIImagePNGRepresentation(image);
   NSFileManager *fileManager = [NSFileManager defaultManager];
   //Save the file, overwrite existing if exists.
   NSLog(@"Attempting to create file at path %@ with %d bytes of data",fullPath, [data length]);
   if ([fileManager createFileAtPath:fullPath contents:data attributes:nil]) {
      NSLog(@"Success");
   } else {
      NSLog(@"Error creating file");
   }
}

在创建图像的类中,我调用了这个方法:

// image is an object of type UIImage
// cachedImageName is a string that resolves to something like User Image/12
[Utilities cacheImage:image usingName:cachedImageName];

以下是调试器中的示例NSLog输出行:

... Caching image User Image/12
... imageCachPath is /var/mobile/Applications/5EBB1152-5CC1-4A30-ABD5-B4C9A60E4CB4/Library/Caches/PieceImages
... File /var/mobile/Applications/5EBB1152-5CC1-4A30-ABD5-B4C9A60E4CB4/Library/Caches/PieceImages exists -- is it a directory? YES
... Attempting to create file at path /var/mobile/Applications/5EBB1152-5CC1-4A30-ABD5-B4C9A60E4CB4/Library/Caches/PieceImages/User_Image/12.png with 12071 bytes of data
... Error creating file

1 个答案:

答案 0 :(得分:7)

如果目录不存在,对NSFileManager fileExistsAtPath:isDirectory:的调用会为isDir提供不确定的值。您应该将代码更改为:

BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:pieceImagesDirectory isDirectory:&isDir]) {
    [[NSFileManager defaultManager]createDirectoryAtPath:pieceImagesDirectory withIntermediateDirectories:YES attributes:nil error:&error];
    NSLog(@"Error after creating directory:\n%@",error);
} else {
    // file exists - I don't expect to use the else block. This is for figuring out what's going on.
    NSLog(@"File %@ exists -- is it a directory? %@",pieceImagesDirectory, isDir?@"YES":@"NO");
}

您还可以在路径中添加第二个文件夹User_Image。你永远不会创建这个目录。

我还建议你改变你写图像数据的方式。不使用NSFileManager createFileAtPath:contents:attributes:,而是使用NSData writeToFile:options:error:。然后你可以得到一个错误对象,提供任何问题的更多细节。

最后,最好构建文件的完整路径。剥去最后一个路径组件(实际文件名),然后检查是否存在剩余路径。然后拨打createDirectoryAtPath...一个电话,让它创建所有需要的文件夹。