-
-
Save hyuni/d3f5949b61ea85458f7b51c7baabf1ac to your computer and use it in GitHub Desktop.
Swift UIImage extension for base64 conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum ImageFormat { | |
case png | |
case jpeg(CGFloat) | |
} | |
extension UIImage { | |
public 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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment