- (void)saveImage:(UIImage *)image{
NSData *imageData = UIImageJPEGRepresentation(image, 0.3);
BOOL success;
NSError *error;
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imgCachePath = @"img-cache";
NSString *imgCacheAbsolutePath = [NSString stringWithFormat:@"%@/html-resources/%@",documentsDirectory,imgCachePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
//判断是否存在改文件夹,不存在创建
success = [fileManager fileExistsAtPath:imgCacheAbsolutePath];
if(!success) {
[fileManager createDirectoryAtPath:imgCacheAbsolutePath withIntermediateDirectories:YES attributes:nil error:&error];
}
// 图片名
NSString *UUIDStr = [[NSUUID UUID] UUIDString];
NSString *imgPath = [NSString stringWithFormat:@"%@/%@.jpg",imgCachePath,UUIDStr];
NSString *imgAbsolutePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/html-resources/%@", imgPath]];
[imageData writeToFile:imgAbsolutePath atomically:YES];
//读取图片文件判断是否存储了该图片
// UIImage *selfPhoto = [UIImage imageWithContentsOfFile:imgAbsolutePath];
}
网友评论