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

ios – 无法在缓存目录中创建文件,第1张

概述我正在尝试设置一个缓存目录,以便在我的应用程序中使用,但由于我不知道的原因,不会创建这些文件.我究竟做错了什么?以下是我正在使用的方法: 在课程实用程序中: +(NSString *)imageCachePath { NSString *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUse 我正在尝试设置一个缓存目录,以便在我的应用程序中使用,但由于我不知道的原因,不会创建这些文件.我究竟做错了什么?以下是我正在使用的方法:

在课程实用程序中:

+(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
解决方法 对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? %@",isDir?@"YES":@"NO");}

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

我还建议你改变你写图像数据的方式.而不是使用NSfileManager createfileAtPath:contents:attributes:,使用NSData writetofile:options:error:.然后,您可以获得一个错误对象,提供任何问题的更多详细信息

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

总结

以上是内存溢出为你收集整理的ios – 无法在缓存目录中创建文件全部内容,希望文章能够帮你解决ios – 无法在缓存目录中创建文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1074500.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存