存储的是文件路径。
把图片转为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:@"full_playlist_hl.png"];
NSData *_data = UIImageJPEGRepresentation(_image, 1.0f);
NSString *_encodedImageStr = [_data base64Encoding];
NSLog(@"===Encoded image:\n%@", _encodedImageStr);
Base64字符串转UIImage图片:
NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:_encodedImageStr];
UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));