IOS图片转base64字符串和字符串转回图片

时间:2021-03-21 21:48:54
    UIImage图片转成base64字符串:
 
UIImage
*_originImage = [UIImage imageNamed:@
"temp.png"];
 
NSData
*_data = UIImageJPEGRepresentation(_image,
1.0f);
 
NSString
*_encodedImageStr = [_data base64Encoding];

Base64字符串转UIImage图片:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
 
UIImage
*_decodedImage      = [UIImage imageWithData:_decodedImageData];
 
NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));


注意:黑体部分也可以用NSData *_decodedImageData   = [[NSData alloc] initWithBase64Encoding:_encodedImageStr];但该方法再ios7中已经废弃。