Convert between UIImage and Base64 string

后端 未结 24 2533
抹茶落季
抹茶落季 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 01:55

    Swift-Extension:

    extension UIImage{
         func toBase64() -> String{
              var imageData = UIImagePNGRepresentation(self)
              return imageData.base64EncodedStringWithOptions(.allZeros)
         }
    }
    
    0 讨论(0)
  • 2020-11-22 01:56

    Swift 4.2 Extension method

    extension UIImage {
        func toBase64() -> String? {
            guard let imageData = self.pngData() else { return nil }
            return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
        }
    }
    

    XCode 9.1 and Swift 4.0

    //
    // Convert UIImage to a base64 representation
    //
    class func convertImageToBase64(image: UIImage) -> String {
        let imageData = UIImagePNGRepresentation(image)!
        return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
    }
    
    //
    // Convert a base64 representation to a UIImage
    //
    class func convertBase64ToImage(imageString: String) -> UIImage {
        let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
        return UIImage(data: imageData)!
    }
    
    0 讨论(0)
  • 2020-11-22 01:56

    It's a very good to understand do you pass prefix as Tyler Sheaffer told. But for some reason you may need this prefix that describe the mime-type in the beginning of the base64 string, so I suggest this piece of code to pass some options before encoding (Swift 5):

    extension UIImage {
    
        enum Format: String {
            case png = "png"
            case jpeg = "jpeg"
        }
    
        func toBase64(type: Format = .jpeg, quality: CGFloat = 1, addMimePrefix: Bool = false) -> String? {
            let imageData: Data?
            switch type {
            case .jpeg: imageData = jpegData(compressionQuality: quality)
            case .png:  imageData = pngData()
            }
            guard let data = imageData else { return nil }
    
            let base64 = data.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
    
            var result = base64
            if addMimePrefix {
                let prefix = "data:image/\(type.rawValue);base64,"
                result = prefix + base64
            }
            return result
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:57
    See my class -  AppExtension.swift
    
    
    // MARK: - UIImage (Base64 Encoding)
    
    public enum ImageFormat {
        case PNG
        case JPEG(CGFloat)
    }
    
    extension UIImage {
    
        public func base64(format: ImageFormat) -> String {
            var imageData: NSData
            switch format {
            case .PNG: imageData = UIImagePNGRepresentation(self)
            case .JPEG(let compression): imageData = UIImageJPEGRepresentation(self, compression)
            }
            return imageData.base64EncodedStringWithOptions(.allZeros)
        }
    }
    
    0 讨论(0)
  • 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()
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 02:00

    Swift version - create base64 for image

    In my opinion Implicitly Unwrapped Optional in case of UIImagePNGRepresenatation() is not safe, so I recommend to use extension like below:

    extension UIImage {
    
        func toBase64() -> String? {
            let imageData = UIImagePNGRepresentation(self)
            return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        }
    }
    
    0 讨论(0)
提交回复
热议问题