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);
相关推荐
杂雾无尘1 小时前
掌握生死时速:苹果应用加急审核全攻略!
ios·swift·apple
晴风向上1 小时前
mac mini m4安装node.js@16以下版本方法
macos·node.js
HarderCoder1 小时前
Swift 6.2 中的 `@concurrent`
ios·swift
JPCstorm2 小时前
Mac homebrew 安装教程
macos
Digitally3 小时前
如何将文件从 iPhone 传输到 Android(新指南)
android·ios·iphone
FreeBuf_3 小时前
朝鲜APT组织使用Nim语言恶意软件对macOS发起隐秘Web3与加密货币攻击
macos·web3·策略模式
YungFan4 小时前
iOS26适配指南之通知
ios·swift
木叶丸4 小时前
跨平台方案该如何选择?
android·前端·ios
我唔知啊5 小时前
OC底层原理二:OC对象的分类(实例对象、类对象、元类对象)
ios·objective-c
泓博6 小时前
KMP(Kotlin Multiplatform)改造(Android/iOS)老项目
android·ios·kotlin