Skip to content

Commit 49844c8

Browse files
feat(helpers): add resize image before compression ✨ (#356)
1 parent 8e0a5b7 commit 49844c8

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

waosSwift/config/default/development.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
],
7575
"img": {
7676
"compresion": 0.25,
77+
"max": 4096,
7778
"styles": {
7879
"blured": 10,
7980
"overlayFraction": 0.9

waosSwift/lib/helpers/Extensions/UIImage.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ extension UIImage {
2727
guard let cgImage2 = ciContext.createCGImage(resultImage, from: inputImage.extent) else { return self }
2828
return UIImage(cgImage: cgImage2)
2929
}
30-
30+
3131
/**
3232
* @desc lighter image
3333
* @param {CGFloat} percentage,
3434
*/
3535
func lighter(by percentage: CGFloat = 30) -> UIImage? {
3636
return self.adjust(by: abs(percentage) )
3737
}
38-
38+
3939
/**
4040
* @desc darker image
4141
* @param {CGFloat} percentage,
4242
*/
4343
func darker(by percentage: CGFloat = 30) -> UIImage? {
4444
return self.adjust(by: -1 * abs(percentage) )
4545
}
46-
46+
4747
/**
4848
* @desc adjust image darkness / lightness from coefficient
4949
* @param {CGFloat} percentage,
@@ -59,7 +59,7 @@ extension UIImage {
5959
guard let cgImage2 = ciContext.createCGImage(resultImage, from: inputImage.extent) else { return self }
6060
return UIImage(cgImage: cgImage2)
6161
}
62-
62+
6363
/**
6464
* @desc adjust image orientation if needed in exif
6565
*/
@@ -75,4 +75,22 @@ extension UIImage {
7575
return result
7676
}
7777
}
78+
79+
/**
80+
* @desc resizeImage width max target Size
81+
*/
82+
func resizeImage(targetSize: CGSize) -> UIImage? {
83+
let size = self.size
84+
let widthRatio = targetSize.width / size.width
85+
let heightRatio = targetSize.height / size.height
86+
let newSize = widthRatio > heightRatio ? CGSize(width: size.width * heightRatio, height: size.height * heightRatio) : CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
87+
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
88+
89+
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
90+
self.draw(in: rect)
91+
let newImage = UIGraphicsGetImageFromCurrentImageContext()
92+
UIGraphicsEndImageContext()
93+
94+
return newImage
95+
}
7896
}

waosSwift/modules/core/controllers/CoreFormController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class CoreFormController: FormViewController {
3838
static let tabBarTitle = NSString(string: config["theme"]["tabBar"]["title"].string ?? "").boolValue
3939
static let tabBarBorder = NSString(string: config["theme"]["tabBar"]["border"].string ?? "").boolValue
4040
static let imgCompression = CGFloat(config["img"]["compresion"].float ?? 1.0)
41+
static let imgMax = CGFloat(config["img"]["max"].float ?? 4096)
4142
static let margin = CGFloat(config["theme"]["global"]["margin"].int ?? 0)
4243
static let radius = CGFloat(config["theme"]["global"]["radius"].int ?? 0)
4344
static let timesButtonsThrottle = Int(config["times"]["buttons"]["throttle"].int ?? 2000)

waosSwift/modules/users/controllers/UserViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class UserViewController: CoreFormController, View {
142142
<<< self.inputAvatar.cellUpdate { cell, _ in
143143
cell.accessoryView?.layer.cornerRadius = (cell.accessoryView?.frame.height ?? 20)/2
144144
}.onChange({ (img) in
145-
if let aux = img.value?.adjustOrientation() {
145+
if let aux = img.value?.adjustOrientation()?.resizeImage(targetSize: CGSize(width: Metric.imgMax, height: Metric.imgMax)) {
146146
self.avatar.accept(aux.jpegData(compressionQuality: Metric.imgCompression))
147147
} else {
148148
self.avatar.accept(nil)

0 commit comments

Comments
 (0)