欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS创建自己本地缓存目录,截图图片存储失败问题

程序员文章站 2022-06-14 15:23:03
...

1. 创建自定义目录时,fileExistsAtPath 必须判断是文件目录

/**
 get absolute iamge cache path
 
 @param imageName file name
 @return absolute path
 */
+ (NSString *)getAbsoluteImageCachePath:(NSString *)imageName {
    NSString *appPackageName = [APP_INFO_DICT objectForKey:@"CFBundleIdentifier"];
    
    NSString *md5ImageName = [self cachedFileNameForKey:imageName];
    NSString *adShowImageDiskPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true) firstObject];
    NSString *adShowImageDiskDir = [adShowImageDiskPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/CZFAdShowImages/", appPackageName]];
    adShowImageDiskPath = [adShowImageDiskDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", md5ImageName]];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:adShowImageDiskDir]) {
        NSError *error;
        [fileManager createDirectoryAtPath:adShowImageDiskDir withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            NSLog(@"create directory error:%@", error.description);
        }
    }
    
    return adShowImageDiskPath;
}

 

相关标签: iOS软件开发