美文网首页
iOS本地文件夹保存图片

iOS本地文件夹保存图片

作者: 淘代码者 | 来源:发表于2017-07-12 14:29 被阅读0次
- (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];
}

相关文章

网友评论

      本文标题:iOS本地文件夹保存图片

      本文链接:https://www.haomeiwen.com/subject/xsjbhxtx.html