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);
相关推荐
imLix6 分钟前
RunLoop 实现原理
前端·ios
肥肥呀呀呀1 小时前
mac 安卓模拟器 blueStacks
macos
csdn_aspnet2 小时前
如何在 MacOS 上安装 SQL Server
macos·sqlserver
归辞...6 小时前
「iOS」————设计架构
ios·架构
i紸定i9 小时前
解决html-to-image在 ios 上dom里面的图片不显示出来
前端·ios·vue·html·html-to-image
YungFan1 天前
iOS26适配指南之UIButton
ios·swift
共享家95271 天前
linux-数据链路层
linux·网络·macos
红橙Darren1 天前
手写操作系统 - 编译链接与运行
android·ios·客户端
鹏多多.1 天前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
CZIDC2 天前
MacOS字体看起来比在 Windows 上更好?
macos