简介:
图片的沙盒读存操作主要是增、删、查,一般不涉及改的操作,这里直接以代码演示
常用代码:
-
增
/** * 存储缩略图到沙盒中 */ + (BOOL)saveImageToPath:(NSString *)imageFilePath image:(UIImage *)image{ return [UIImageJPEGRepresentation(image, 0.5) writeToFile:imageFilePath atomically:YES]; }
-
删
/** * 删除沙盒里的缩略图 */ + (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); } } }
-
查
UIImage *image = [UIImage imageWithContentsOfFile:thumbnailPath];
-
获取缩略图路径
/** * 获取缩略图的路径 */ + (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; }
-
获取沙盒根目录
//获取沙盒根目录
NSString *directory = NSHomeDirectory();
NSLog(@"directory:%@", directory);
-
获取Document路径
//获取Documents路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
-
获取Library路径
//获取Library路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSLog(@"path:%@", path);
-
获取tmp路径
NSString *tmp = NSTemporaryDirectory(); NSLog(@"tmp:%@", tmp);
-
获取Caches路径
//获取Caches路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSLog(@"path:%@", path);