|
@@ -37,10 +37,6 @@ public extension UIImage {
|
|
return newImage
|
|
return newImage
|
|
}
|
|
}
|
|
|
|
|
|
- func dcCompress(toMax target: Float = 1280) -> UIImage? {
|
|
|
|
- return scaleDownAndCompress(toMax: target)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
func imageSizeInPixel() -> CGSize {
|
|
func imageSizeInPixel() -> CGSize {
|
|
let heightInPoints = size.height
|
|
let heightInPoints = size.height
|
|
let heightInPixels = heightInPoints * scale
|
|
let heightInPixels = heightInPoints * scale
|
|
@@ -48,26 +44,6 @@ public extension UIImage {
|
|
let widthInPixels = widthInPoints * scale
|
|
let widthInPixels = widthInPoints * scale
|
|
return CGSize(width: widthInPixels, height: heightInPixels)
|
|
return CGSize(width: widthInPixels, height: heightInPixels)
|
|
}
|
|
}
|
|
-
|
|
|
|
- // if an image has an alpha channel we try to keep it, using PNG formatting instead of JPEG
|
|
|
|
- // PNGs are less compressed than JPEGs - to keep the message sizes small,
|
|
|
|
- // the size of PNG imgaes will be scaled down
|
|
|
|
- func scaleDownAndCompress(toMax: Float) -> UIImage? {
|
|
|
|
- let rect = getResizedRectangle(toMax: self.isTransparent() ?
|
|
|
|
- min(Float(self.size.width) / 2, toMax / 2) :
|
|
|
|
- toMax)
|
|
|
|
-
|
|
|
|
- UIGraphicsBeginImageContextWithOptions(rect.size, !self.isTransparent(), 0.0)
|
|
|
|
- draw(in: rect)
|
|
|
|
- let img = UIGraphicsGetImageFromCurrentImageContext()
|
|
|
|
-
|
|
|
|
- let imageData = self.isTransparent() ?
|
|
|
|
- img?.pngData() :
|
|
|
|
- img?.jpegData(compressionQuality: 0.85)
|
|
|
|
-
|
|
|
|
- UIGraphicsEndImageContext()
|
|
|
|
- return UIImage(data: imageData!)
|
|
|
|
- }
|
|
|
|
|
|
|
|
func isTransparent() -> Bool {
|
|
func isTransparent() -> Bool {
|
|
guard let alpha: CGImageAlphaInfo = self.cgImage?.alphaInfo else { return false }
|
|
guard let alpha: CGImageAlphaInfo = self.cgImage?.alphaInfo else { return false }
|