Convert between UIImage and Base64 string
是否有人知道如何将
我有下面的代码;编码前的原始图像是好的,但是我只有在编码和解码后才能得到空白图像。
1 2 3 4 5 | NSData *imageData = UIImagePNGRepresentation(viewImage); NSString *b64EncStr = [self encode: imageData]; NSString *base64String = [self encodeBase64:imageData]; |
迅捷
首先我们需要图像的nsdata
1 2 3 4 5 6 7 8 9 10 11 | //Use image name from bundle to create NSData let image : UIImage = UIImage(named:"imageNameHere")! //Now use image to create into NSData format let imageData:NSData = UIImagePNGRepresentation(image)! //OR next possibility //Use image's path to create NSData let url:NSURL = NSURL(string :"urlHere")! //Now use image to create into NSData format let imageData:NSData = NSData.init(contentsOfURL: url)! |
Swift 2.0>编码
1 | let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) |
Swift 2.0>解码
1 | let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)! |
Swift 3.0>解码
1 | let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)! |
编码:
1 2 | let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters) print(strBase64) |
解码:
1 2 3 4 | let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))! let decodedimage:UIImage = UIImage(data: dataDecoded)! print(decodedimage) yourImageView.image = decodedimage |
斯威夫特3
1 2 3 | let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)! let decodedimage = UIImage(data: dataDecoded) yourImageView.image = decodedimage |
Objtovi-CiOS7>版本
您可以使用
编码:
1 2 3 | - (NSString *)encodeToBase64String:(UIImage *)image { return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; } |
解码:
1 2 3 4 | - (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData { NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters]; return [UIImage imageWithData:data]; } |
iOS 6.1及以下版本
第一个选项:使用指向
在您的
编码:
1 2 | NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f); NSString *strEncoded = [Base64 encode:data]; |
解码:
1 2 | NSData* data = [Base64 decode:strEncoded ];; image.image = [UIImage imageWithData:data]; |
另一种选择:对
Swift 4.2扩展方法
1 2 3 4 5 6 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // // 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)! } |
Swift 3.0,Xcode 8.0
用URL替换字符串。测试图像是图像视图的一个出口
1 2 3 4 5 6 7 8 9 10 11 12 | // Put Your Image URL let url:NSURL = NSURL(string :"http://.jpg")! // It Will turn Into Data let imageData : NSData = NSData.init(contentsOf: url as URL)! // Data Will Encode into Base64 let str64 = imageData.base64EncodedData(options: .lineLength64Characters) // Now Base64 will Decode Here let data: NSData = NSData(base64Encoded: str64 , options: .ignoreUnknownCharacters)! // turn Decoded String into Data let dataImage = UIImage(data: data as Data) // pass the data image to image View.:) testImage.image = dataImage |
希望能有所帮助,谢谢。
斯威夫特IOS8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // prgm mark ---- // convert images into base64 and keep them into string func convertImageToBase64(image: UIImage) -> String { var imageData = UIImagePNGRepresentation(image) let base64String = imageData.base64EncodedStringWithOptions(.allZeros) return base64String }// end convertImageToBase64 // prgm mark ---- // convert images into base64 and keep them into string func convertBase64ToImage(base64String: String) -> UIImage { let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0) ) var decodedimage = UIImage(data: decodedData!) return decodedimage! }// end convertBase64ToImage |
斯威夫特3
将图像转换为base64字符串
操场测试
1 2 3 4 | var logo = UIImage(named:"image_logo") let imageData:Data = UIImagePNGRepresentation(logo) let base64String = imageData.base64EncodedString() print(base64String) |
1 2 3 4 5 6 7 8 | @implementation UIImage (Extended) - (NSString *)base64String { NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength]; return [NSString stringWithUTF8String:[data bytes]]; } @end |
在Swift 2.0中使用此扩展名(贷记给Jonas Franz)
1 2 3 4 5 6 | extension UIImage{ func toBase64() -> String{ let imageData = UIImagePNGRepresentation(self)! return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) } } |
在Swift 3.0和Xcode 8.0中编码:
1 2 3 | let userImage:UIImage = UIImage(named:"Your-Image_name")! let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData let dataImage = imageData.base64EncodedString(options: .lineLength64Characters) |
解码:
1 2 3 4 | let imageData = dataImage let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)! let avatarImage:UIImage = UIImage(data: dataDecode as Data)! yourImageView.image = avatarImage |
斯威夫特5
编码
1 2 3 | func ConvertImageToBase64String (img: UIImage) -> String { return img.jpegData(compressionQuality: 1)?.base64EncodedString() ??"" } |
译码
1 2 3 4 5 | func ConvertBase64StringToImage (imageBase64String:String) -> UIImage { let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0)) let image = UIImage(data: imageData!) return image! } |
Note: Tested in xcode 10.2
斯威夫特4
编码
1 2 3 4 5 | func ConvertImageToBase64String (img: UIImage) -> String { let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img) let imgString = imageData.base64EncodedString(options: .init(rawValue: 0)) return imgString } |
译码
1 2 3 4 5 | func ConvertBase64StringToImage (imageBase64String:String) -> UIImage { let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0)) let image = UIImage(data: imageData!) return image } |
Note: Tested in xcode 9.4.1
SWIFT扩展:
1 2 3 4 5 6 | extension UIImage{ func toBase64() -> String{ var imageData = UIImagePNGRepresentation(self) return imageData.base64EncodedStringWithOptions(.allZeros) } } |
斯威夫特4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | enum ImageFormat { case png case jpeg(CGFloat) } extension UIImage { func base64(format: ImageFormat) -> String? { var imageData: Data? switch format { case .png: imageData = UIImagePNGRepresentation(self) case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression) } return imageData?.base64EncodedString() } } extension String { func imageFromBase64() -> UIImage? { guard let data = Data(base64Encoded: self) else { return nil } return UIImage(data: data) } } |
Swift版本-为图像创建base64
在我看来,在
1 2 3 4 5 6 7 | extension UIImage { func toBase64() -> String? { let imageData = UIImagePNGRepresentation(self) return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) } } |
我尝试了所有的解决方案,没有一个对我有效(使用swift 4),这是对我有效的解决方案,如果将来有人面临同样的问题。
1 2 3 4 5 6 | let temp = base64String.components(separatedBy:",") let dataDecoded : Data = Data(base64Encoded: temp[1], options: .ignoreUnknownCharacters)! let decodedimage = UIImage(data: dataDecoded) yourImage.image = decodedimage |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | See my class - AppExtension.swift // MARK: - UIImage (Base64 Encoding) public enum ImageFormat { case PNG case JPEG(CGFloat) } extension UIImage { public func base64(format: ImageFormat) -> String { var imageData: NSData switch format { case .PNG: imageData = UIImagePNGRepresentation(self) case .JPEG(let compression): imageData = UIImageJPEGRepresentation(self, compression) } return imageData.base64EncodedStringWithOptions(.allZeros) } } |
Swift 4.2_xcode 10
1 2 3 4 5 6 7 | extension UIImage { /// EZSE: Returns base64 string public var base64: String { return self.jpegData(compressionQuality: 1.0)!.base64EncodedString() } } |
在斯威夫特3
1 2 3 4 5 6 7 | func decodeBase64(toImage strEncodeData: String) -> UIImage { let dataDecoded = NSData(base64Encoded: strEncodeData, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! let image = UIImage(data: dataDecoded as Data) return image! } |
Swift 5,Xcode 10.
1 2 3 | let imageData = UIImage(named:"imagename").pngData()?.base64EncodedString(options: .lineLength64Characters) print(imageData) |
Swift 3.0和Xcode 8.0
1 2 3 4 | let imageData = UIImageJPEGRepresentation(imageView.image!, 1) let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) print(base64String) |
对于iOS 7+、Objective-C,下面介绍如何从图像URL开始转换:
1 2 3 4 5 6 | NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; NSString *base64String = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; |