Convert between UIImage and Base64 string

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

    Swift 5.

    class ImageConverter {
    
        func base64ToImage(_ base64String: String) -> UIImage? {
            guard let imageData = Data(base64Encoded: base64String) else { return nil }
            return UIImage(data: imageData)
        }
    
        func imageToBase64(_ image: UIImage) -> String? {
            return image.jpegData(compressionQuality: 1)?.base64EncodedString()
        }
    
    }
    

提交回复
热议问题