项目中可能会有这样的需求,比如图片上传服务器时一般都会转成NSString再上传
那么问题来了,图片如何转为NSString ,已经被转为NSString的图片如何再转成图片
其实很简单,给大家两个方法就ok
- //图片转字符串
- -(NSString *)UIImageToBase64Str:(UIImage *) image
- {
- NSData *data = UIImageJPEGRepresentation(image, 1.0f);
- NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
- return encodedImageStr;
- }
- //字符串转图片
- -(UIImage *)Base64StrToUIImage:(NSString *)_encodedImageStr
- {
- NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:_encodedImageStr];
- UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
- return _decodedImage;
- }