iOS 沙盒图片的存取

简介:

图片的沙盒读存操作主要是增、删、查,一般不涉及改的操作,这里直接以代码演示

常用代码:
  1.  /**
      *  存储缩略图到沙盒中
      */
     + (BOOL)saveImageToPath:(NSString *)imageFilePath image:(UIImage *)image{
         return [UIImageJPEGRepresentation(image, 0.5) writeToFile:imageFilePath  atomically:YES];
     }
    
  2.  /**
      *  删除沙盒里的缩略图
      */
     + (void)deleteImageWithPath:(NSString *)thumbnailPath{
         BOOL isHave = [[NSFileManager defaultManager] fileExistsAtPath:thumbnailPath];
    
         if (!isHave) {
             NSLog(@"no  have");
             return ;
         }else {
             NSError *error;
             BOOL isDeleteFinish = [[NSFileManager defaultManager] removeItemAtPath:thumbnailPath error:&error];
     
             if (!isDeleteFinish) {
                 NSLog(@"delete fail --- %@", error);
             }
         }
     }
    
  3.  UIImage *image = [UIImage imageWithContentsOfFile:thumbnailPath];
    
  4. 获取缩略图路径

     /**
      *  获取缩略图的路径
      */
     + (NSString *)getFilePath{
         NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
         NSString *thumbnailPath = [NSString stringWithFormat:@"%@/Caches/AR/ARThumbnail", libPath];
         NSFileManager *fileManager = [NSFileManager defaultManager];
         if (![fileManager fileExistsAtPath:thumbnailPath]) {
             NSError *error;
             [fileManager createDirectoryAtPath:thumbnailPath withIntermediateDirectories:YES attributes:nil error:&error];
             if (error) {
                 NSLog(@"error");
                 return nil;
             }
         }        
         return thumbnailPath;
     }
    
  5. 获取沙盒根目录

    //获取沙盒根目录

    NSString *directory = NSHomeDirectory();

    NSLog(@"directory:%@", directory);

  6. 获取Document路径

    //获取Documents路径

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *path = [paths objectAtIndex:0];

    NSLog(@"path:%@", path);

  7. 获取Library路径

      //获取Library路径
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
      NSString *path = [paths objectAtIndex:0];
      NSLog(@"path:%@", path);
    
  8. 获取tmp路径

      NSString *tmp = NSTemporaryDirectory();
      NSLog(@"tmp:%@", tmp);
    
  9. 获取Caches路径

      //获取Caches路径
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
      NSString *path = [paths objectAtIndex:0];
      NSLog(@"path:%@", path);
    
相关推荐
李小苶10 小时前
Mac m1,m2,m3芯片使用nvm安装node14报错
macos
taopi202411 小时前
ios打包:uuid与udid
ios·iphone·ipad
我是唐青枫13 小时前
Mac 上如何同时运行多个MySQL版本?
mysql·macos·adb
好悬给我拽开线15 小时前
【论文阅读】RT-SKETCH: GOAL-CONDITIONED IMITATION LEARNING FROM HAND-DRAWN SKETCHES
论文阅读·macos·sketch
小鹿撞出了脑震荡21 小时前
Effective Objective-C 2.0 读书笔记——关联对象
开发语言·ios·objective-c
小鹿撞出了脑震荡21 小时前
Effective Objective-C 2.0 读书笔记—— objc_msgSend
ios·objective-c·xcode
fareast_mzh21 小时前
Customize ringtone on your iPhone
ios·iphone
随心但不率性1 天前
macos app签名和公证
macos·策略模式
Mr.L705172 天前
Maui学习笔记- SQLite简单使用案例02添加详情页
笔记·学习·ios·sqlite·c#
taopi20242 天前
ios swift画中画技术尝试
ios·xcode·swift