Swift base64转成图片以及字符串,以及图片和字符串转成 base64的方法

时间:2022-08-19 21:48:17

图片转成 base64:

let image : UIImage =UIImage(named:"test.png")!

let imageData = UIImagePNGRepresentation(image)

let base64String = imageData!.base64EncodedStringWithOptions

(NSDataBase64EncodingOptions(rawValue:0))


base 转成图片:

let decodedData = NSData(base64EncodedString:base64String, options:NSDataBase64DecodingOptions())

let decodedimage = UIImage(data: decodedData!) as UIImage


字符串转 base64

   let  base64String ="字符串"

   let data = base64String.dataUsingEncoding(NSUTF8StringEncoding)

   let base64 = data?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.init(rawValue: 0)     


base64 转 字符串

let decodedData:NSData? =NSData(base64EncodedString:"base64字符串", options: .IgnoreUnknownCharacters)

        

 let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)!