Convert between UIImage and Base64 string

后端 未结 24 2572
抹茶落季
抹茶落季 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:21

    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
    

提交回复
热议问题