在UIImage和Base64string之间转换

有谁知道如何将UIImage转换为Base64string,然后将其反转?

我有下面的代码; 编码之前的原始图像是好的,但我编码和解码后,我只得到一个空白的图像。

 NSData *imageData = UIImagePNGRepresentation(viewImage); NSString *b64EncStr = [self encode: imageData]; NSString *base64String = [self encodeBase64:imageData]; 

迅速

首先,我们需要有图像的NSData

 //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>编码

 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 

Objective-C的

iOS7>版本

你可以使用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和<版本

第一个选项 :使用此链接decode图像进行encodedecode

在您的project添加Base64类。

编码

  NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f); NSString *strEncoded = [Base64 encode:data]; 

解码

  NSData* data = [Base64 decode:strEncoded ];; image.image = [UIImage imageWithData:data]; 

另一个选项 :使用QSUTILITY进行encodingdecoding


SWIFT 3.0,XCODE 8.0

将Stringreplace为您的URL。 testImage是ImageView的出口

 // 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 

希望它帮助谢谢。

Swift iOS8

 // 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 
 @implementation UIImage (Extended) - (NSString *)base64String { NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength]; return [NSString stringWithUTF8String:[data bytes]]; } @end 

在swift 2.0中使用这个扩展名(功劳乔纳斯·弗朗茨)

 extension UIImage{ func toBase64() -> String{ let imageData = UIImagePNGRepresentation(self)! return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) } } 

斯威夫特 – 扩展:

 extension UIImage{ func toBase64() -> String{ var imageData = UIImagePNGRepresentation(self) return imageData.base64EncodedStringWithOptions(.allZeros) } } 

在Swift 3.0和Xcode 8.0中

编码:

 let userImage:UIImage = UIImage(named: "Your-Image_name")! let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData let dataImage = imageData.base64EncodedString(options: .lineLength64Characters) 

解码:

 let imageData = dataImage let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)! let avatarImage:UIImage = UIImage(data: dataDecode as Data)! yourImageView.image = avatarImage 

Swift 3.0

将图像转换为base64string

在操场上testing

  var logo = UIImage(named: "image_logo") let imageData:Data = UIImagePNGRepresentation(logo) let base64String = imageData.base64EncodedString() print(base64String) 
 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版本 – 为图像创buildbase64

在我看来,在UIImagePNGRepresenatation()情况下隐式解包可选是不安全的,所以我推荐使用如下所示的扩展:

 extension UIImage { func toBase64() -> String? { let imageData = UIImagePNGRepresentation(self) return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) } } 

在Swift 3.0中

 func decodeBase64(toImage strEncodeData: String) -> UIImage { let dataDecoded = NSData(base64Encoded: strEncodeData, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! let image = UIImage(data: dataDecoded as Data) return image! } 

对于iOS 7+,Objective-C,下面是如何从图片url开始转换:

 NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; NSString *base64String = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 

Swift 3.0和Xcode 8.0

 let imageData = UIImageJPEGRepresentation(imageView.image!, 1) let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) print(base64String)