IOS之文件夹创建、删除,图片在本地的保存和加载

[html]  view plain copy
  1. // get file absolutely path in the caches directory  
  2.  NSString* pathInCacheDirectory(NSString *fileName)  
  3.  {  
  4.      NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  5.      NSString *cachePath = [cachePaths objectAtIndex:0];  
  6.      return [cachePath stringByAppendingPathComponent:fileName];  
  7.  }  

[html]  view plain copy
  1. // create directory in the caches directory  
  2.  bool createDirInCache(NSString *dirName)  
  3.  {  
  4.      NSString *imageDir = pathInCacheDirectory(dirName);  
  5.      BOOL isDir = NO;  
  6.      NSFileManager *fileManager = [NSFileManager defaultManager];  
  7.      BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];  
  8.      bool isCreated = false;  
  9.      if ( !(isDir == YES && existed == YES) )  
  10.      {  
  11.          isCreated = [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];  
  12.      }  
  13.      return isCreated;  
  14.  }  
  15.    
  16.  // delete directory in the caches directory  
  17.  bool deleteDirInCache(NSString *dirName)  
  18.  {  
  19.      NSString *imageDir = pathInCacheDirectory(dirName);  
  20.      BOOL isDir = NO;  
  21.      NSFileManager *fileManager = [NSFileManager defaultManager];  
  22.      BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];  
  23.      bool isDeleted = false;  
  24.      if ( isDir == YES && existed == YES )  
  25.      {  
  26.          isDeleted = [fileManager removeItemAtPath:imageDir error:nil];  
  27.      }  
  28.    
  29.      return isDeleted;  
  30.  }  
  31.    
  32.  // save Image to the caches directory  
  33.  bool saveImageToCacheDir(NSString *directoryPath, UIImage *image, NSString *imageName, NSString *imageType)  
  34.  {  
  35.      BOOL isDir = NO;  
  36.      NSFileManager *fileManager = [NSFileManager defaultManager];  
  37.      BOOL existed = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];  
  38.      bool isSaved = false;  
  39.      if ( isDir == YES && existed == YES )  
  40.      {  
  41.          if ([[imageType lowercaseString] isEqualToString:@"png"])  
  42.          {  
  43.              isSaved = [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];  
  44.          }  
  45.          else if ([[imageType lowercaseString] isEqualToString:@"jpg"] || [[imageType lowercaseString] isEqualToString:@"jpeg"])  
  46.          {  
  47.              isSaved = [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];  
  48.          }  
  49.          else  
  50.          {  
  51.              NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", imageType);  
  52.          }  
  53.      }  
  54.      return isSaved;  
  55.  }  
  56.    
  57.  // load Image from caches dir to imageview  
  58.  NSData* loadImageData(NSString *directoryPath, NSString *imageName)  
  59.  {  
  60.      BOOL isDir = NO;  
  61.      NSFileManager *fileManager = [NSFileManager defaultManager];  
  62.      BOOL dirExisted = [fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];  
  63.      if ( isDir == YES && dirExisted == YES )  
  64.      {  
  65.          NSString *imagePath = [directoryPath stringByAppendingString : imageName];  
  66.          BOOL fileExisted = [fileManager fileExistsAtPath:imagePath];  
  67.          if (!fileExisted) {  
  68.              return NULL;  
  69.          }  
  70.          NSData *imageData = [NSData dataWithContentsOfFile : imagePath];  
  71.          return imageData;  
  72.      }  
  73.      else  
  74.      {  
  75.          return NULL;  
  76.      }  
  77.  }  
  78.    
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值