代码示例:
// NSData转NSString-base64
UIImage *image = [UIImage imageNamed:@"01"];
NSData *data = UIImagePNGRepresentation(image);
self.base64Image = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
base64 = iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAAAXNSR0IArs4c6QAA
ABxpRE9UAAAAAgAAAAAAAACAAAAAKAAAAIAAAACAAABwJTIJYZEAAEAASURBVHgB
7H0HWJVXuu49954z95wnZ2bu9JOZFGOPBSQ0kSIqYu/BRgQLo2BBBY0CGsFCEJEA……
// NSString-base64转NSData,并保存在本地
NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [Paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:@"image"];
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];
NSString *fileName = [NSString stringWithFormat:@"%@%@", @(arc4random() % 1000 + 1), @".png"];
self.filePath = [path stringByAppendingPathComponent:fileName];
// 保存
NSData *data = [[NSData alloc] initWithBase64EncodedString:self.base64Image options:NSDataBase64DecodingIgnoreUnknownCharacters];
[data writeToFile:self.filePath atomically:NO];
filePath = /Users/zhangshaoyu/Library/Developer/CoreSimulator/Devices/4C5050F0-FFE6-4185-AD76-C54B797596BF/data/Containers/Data/Application/32DBEEB6-5F60-4FCC-8909-48302776141F/Documents/image/29.png
// 读取本地文件
UIImage *image = [UIImage imageWithContentsOfFile:self.filePath];
self.footerView.image = image;
网友评论