把图片转为base64的字符串存到数据库中或者plist文件中,然后用到的时候再取出来
//获取沙盒路径,
NSString *path_sandox = NSHomeDirectory();
//创建一个存储plist文件的路径
NSString *newPath = [path_sandox stringByAppendingPathComponent:@"/Documents/pic.plist"];
NSMutableArray *arr = [[NSMutableArray alloc] init];
//把图片转换为Base64的字符串
NSString *image64 = [self encodeToBase64String:image];
[arr addObject:image64];
//写入plist文件
if ([arr writeToFile:newPath atomically:YES]) {
NSLog(@"写入成功");
};
这样就存起来的,然后用到的时候再利用存储的字符串转化为图片。
NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:image64];
UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
//UIImage图片转成Base64字符串:
UIImage *originImage = [UIImage imageNamed:@"originImage.png"];
NSData *data = UIImageJPEGRepresentation(originImage, 1.0f);
NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//Base64字符串转UIImage图片:
NSData *decodedImageData = [[NSData alloc]
initWithBase64EncodedString:encodedImageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *decodedImage = [UIImage imageWithData:decodedImageData];