Convert between UIImage and Base64 string

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

    For the Base64 code like:

    "data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQAAAACFI5MzAAAB9klEQVR42u2YQYorMQxEBbqWQFc36FoG/6pyOpNZ/J20mGGaTiftF2hbLpWU2PnfYX/k55Jl5vhUVTu8luUdaCeFcydejjdwDUyQ5XV2JOcSZnkHZgiejusK51QGycrl2yIR1BwjjKivSFz8YC7fY91GKIj6PL5pp4/wWL54t3MHt/AjFxoJwmkYwosbh6/UEHE817hvi/vGex8gEkTdVRo1/55BM7kjUIgpoMW1DxB6kD+GtCX4PUFws40OwcUm0/lRYjOB3pG9YcguBFQuO0ISJ9UIrUP5CKy/MriXHDkETYmLDax1+RkgWBglQgUyq6T/HCAHBq7iJHd9KWWAlIKoGpiLc6HNDhDkETNYwqeVhym72snKKxA6BJL4UPM5QPYtgGwZeNZ5O0UvgSb0VGdcmVfJCQwQrM+pRiGnYJ497SUlv2NOYfOCX3qU2Equ7W3JAslsN7oDBDWWojcZq+KbEwQRdRYl1wD3ML52rpGc6w24qCXaKh4DRHWJbUPemqtEGyBMKC4Q/QmWiDWzRxkgO1UtSLh3svMaILeDpEGwrwvZ4Bkg9LynK1Y1LJWQdqKGnm3K7VTCz7vS9hIuUyYRd/xKcYRIHGqAViisQ4S/Uozmqo41Pn6bNRI1xS/fk2fMEKpDZYkpjP6B1T0HyN9/Nb+M/AORXDdE4Lb/mQAAAABJRU5ErkJggg=="

    Use Swift5.0 code like:

    func imageFromBase64(_ base64: String) -> UIImage? {
        if let url = URL(string: base64) {
            if let data = try? Data(contentsOf: url) {
                return UIImage(data: data)
            }
        }
        return nil
    }
    
    0 讨论(0)
  • 2020-11-22 02:19

    Swift 4

    enum ImageFormat {
        case png
        case jpeg(CGFloat)
    }
    
    extension UIImage {
        func base64(format: ImageFormat) -> String? {
            var imageData: Data?
    
            switch format {
            case .png: imageData = UIImagePNGRepresentation(self)
            case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
            }
    
            return imageData?.base64EncodedString()
        }
    }
    
    extension String {
        func imageFromBase64() -> UIImage? {
            guard let data = Data(base64Encoded: self) else { return nil }
    
            return UIImage(data: data)
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:19
    //convert Image to Base64 (Encoding)
    
    let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
    
    print(strBase64)
    
    // convert Base64 to Image (Decoding)
    
    let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
    
    let decodedimage:UIImage = UIImage(data: dataDecoded)!
    
    print(decodedimage)
    
    yourImageView.image = decodedimage
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 02:21

    Swift 4.2 | Xcode 10

    extension UIImage {
    
        /// EZSE: Returns base64 string
        public var base64: String {
            return self.jpegData(compressionQuality: 1.0)!.base64EncodedString()
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:22

    I tried all the solutions, none worked for me (using Swift 4), this is the solution that worked for me, if anyone in future faces the same problem.

    let temp = base64String.components(separatedBy: ",")
    let dataDecoded : Data = Data(base64Encoded: temp[1], options: 
     .ignoreUnknownCharacters)!
    let decodedimage = UIImage(data: dataDecoded)
    
    yourImage.image = decodedimage
    
    0 讨论(0)
提交回复
热议问题