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);
    
相关推荐
CYBEREXP20084 小时前
MacOS M3源代码编译Qt6.8.1
c++·qt·macos
crasowas8 小时前
iOS - 超好用的隐私清单修复脚本(持续更新)
ios·app store
ii_best10 小时前
ios按键精灵脚本开发:ios悬浮窗命令
ios
Code&Ocean15 小时前
iOS从Matter的设备认证证书中获取VID和PID
ios·matter·chip
/**书香门第*/15 小时前
Laya ios接入goole广告,开始接入 2
ios
SoraLuna1 天前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
阿7_QuQ1 天前
怎么在Windows上远程控制Mac电脑?
macos
小路恢弘1 天前
使用Mac自带共享实现远程操作
macos
恋猫de小郭1 天前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
网安墨雨1 天前
iOS应用网络安全之HTTPS
web安全·ios·https