UIImage和base 64字符串之间的转换是否有人知道如何将UIImage到一个基本64字符串,然后反转它?我有下面的代码;编码前的原始图像很好,但只有在编码和解码之后才能得到空白的图像。NSData *imageData = UIImagePNGRepresentation(viewImage);NSString *b64EncStr = [self encode: imageData];
NSString *base64String = [self encodeBase64:imageData];
3 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
首先,我们需要图像的NSData
//Use image name from bundle to create NSDatalet image : UIImage = UIImage(named:"imageNameHere")!//Now use image to create into NSData formatlet imageData:NSData = UIImagePNGRepresentation(image)!//OR next possibility//Use image's path to create NSDatalet url:NSURL = NSURL(string : "urlHere")!//Now use image to create into NSData formatlet imageData:NSData = NSData.init(contentsOfURL: url)!
SWIFT 2.0>编码
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
SWIFT 2.0>解码
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
SWIFT 3.0>解码
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
编码 :
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)print(strBase64)
解码 :
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!let decodedimage:UIImage = UIImage(data: dataDecoded)!print(decodedimage)yourImageView.image = decodedimage
SWIFT 3.0
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!let decodedimage = UIImage(data: dataDecoded) yourImageView.image = decodedimage
目标-C
iOS 7>版本
NSData's
base64EncodedStringWithOptions
编码 :
- (NSString *)encodeToBase64String:(UIImage *)image { return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];}
解码 :
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData { NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters]; return [UIImage imageWithData:data];}
IOS 6.1和<版本
第一选择encode
decode
project
.
编码 :
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f); NSString *strEncoded = [Base64 encode:data];
解码 :
NSData* data = [Base64 decode:strEncoded ];; image.image = [UIImage imageWithData:data];
另一种选择encoding
decoding
弑天下
TA贡献1818条经验 获得超8个赞
SWIFT 4.2扩展方法
extension UIImage { func toBase64() -> String? { guard let imageData = self.pngData() else { return nil } return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters) }}
Xcode 9.1和SWIFT 4.0
// // Convert UIImage to a base64 representation //class func convertImageToBase64(image: UIImage) -> String { let imageData = UIImagePNGRepresentation(image)! return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)}// // Convert a base64 representation to a UIImage //class func convertBase64ToImage(imageString: String) -> UIImage { let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)! return UIImage(data: imageData)!}
- 3 回答
- 0 关注
- 912 浏览
添加回答
举报
0/150
提交
取消