Last active
April 16, 2022 09:04
-
-
Save meyusufdemirci/311ae78b0ebb0d49e6a64970a453821c to your computer and use it in GitHub Desktop.
Compress Image By Max Kb
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
import UIKit | |
extension UIImage { | |
func compress(maxKb: Double) -> Data? { | |
let quality: CGFloat = maxKb / self.sizeAsKb() | |
let compressedData: Data? = self.jpegData(compressionQuality: quality) | |
return compressedData | |
} | |
func sizeAsKb() -> Double { | |
Double(self.pngData()?.count ?? 0 / 1024) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment