Convert between UIImage and Base64 string

后端 未结 24 2550
抹茶落季
抹茶落季 2020-11-22 01:45

Does anyone know how to convert a UIImage to a Base64 string, and then reverse it?

I have the below code; the original image before encoding is good, bu

24条回答
  •  抹茶落季
    2020-11-22 02:04

    SWIFT 3.0, XCODE 8.0

    Replace String with your URL. and testImage is an outlet of 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
    

    Hope It Helps Thanks.

提交回复
热议问题