最近遇到一個(gè)需求伯襟,要將一張圖片轉(zhuǎn)換成base64的格式续室,上傳到后臺(tái),由于之前沒(méi)做過(guò)沽甥,所以這里記錄一下在網(wǎng)上找的方法桶蝎。
官方文檔相關(guān)資料
- UIImage轉(zhuǎn)base64
//將圖片轉(zhuǎn)成base64格式
let imgData = UIImageJPEGRepresentation(UIImage.init("hhh"), 1.0)
let baseImg = imgData?.base64EncodedDataWithOptions(.Encoding64CharacterLineLength)
- base64轉(zhuǎn)UIImage
//base64轉(zhuǎn)image
let data = NSData.init(base64EncodedData: baseImg!, options: .IgnoreUnknownCharacters)
let img = UIImage.init(data: data!)
let imgV = UIImageView.init(image: img!)
imgV.frame = CGRect(x: 10, y: 300, width: self.kScreenWidth, height: 200)
imgV.contentMode = .ScaleAspectFit
self.view.addSubview(imgV)