Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions waosSwift/config/default/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
],
"img": {
"compresion": 0.25,
"max": 4096,
"styles": {
"blured": 10,
"overlayFraction": 0.9
Expand Down
26 changes: 22 additions & 4 deletions waosSwift/lib/helpers/Extensions/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ extension UIImage {
guard let cgImage2 = ciContext.createCGImage(resultImage, from: inputImage.extent) else { return self }
return UIImage(cgImage: cgImage2)
}

/**
* @desc lighter image
* @param {CGFloat} percentage,
*/
func lighter(by percentage: CGFloat = 30) -> UIImage? {
return self.adjust(by: abs(percentage) )
}

/**
* @desc darker image
* @param {CGFloat} percentage,
*/
func darker(by percentage: CGFloat = 30) -> UIImage? {
return self.adjust(by: -1 * abs(percentage) )
}

/**
* @desc adjust image darkness / lightness from coefficient
* @param {CGFloat} percentage,
Expand All @@ -59,7 +59,7 @@ extension UIImage {
guard let cgImage2 = ciContext.createCGImage(resultImage, from: inputImage.extent) else { return self }
return UIImage(cgImage: cgImage2)
}

/**
* @desc adjust image orientation if needed in exif
*/
Expand All @@ -75,4 +75,22 @@ extension UIImage {
return result
}
}

/**
* @desc resizeImage width max target Size
*/
func resizeImage(targetSize: CGSize) -> UIImage? {
let size = self.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
let newSize = widthRatio > heightRatio ? CGSize(width: size.width * heightRatio, height: size.height * heightRatio) : CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CoreFormController: FormViewController {
static let tabBarTitle = NSString(string: config["theme"]["tabBar"]["title"].string ?? "").boolValue
static let tabBarBorder = NSString(string: config["theme"]["tabBar"]["border"].string ?? "").boolValue
static let imgCompression = CGFloat(config["img"]["compresion"].float ?? 1.0)
static let imgMax = CGFloat(config["img"]["max"].float ?? 4096)
static let margin = CGFloat(config["theme"]["global"]["margin"].int ?? 0)
static let radius = CGFloat(config["theme"]["global"]["radius"].int ?? 0)
static let timesButtonsThrottle = Int(config["times"]["buttons"]["throttle"].int ?? 2000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class UserViewController: CoreFormController, View {
<<< self.inputAvatar.cellUpdate { cell, _ in
cell.accessoryView?.layer.cornerRadius = (cell.accessoryView?.frame.height ?? 20)/2
}.onChange({ (img) in
if let aux = img.value?.adjustOrientation() {
if let aux = img.value?.adjustOrientation()?.resizeImage(targetSize: CGSize(width: Metric.imgMax, height: Metric.imgMax)) {
self.avatar.accept(aux.jpegData(compressionQuality: Metric.imgCompression))
} else {
self.avatar.accept(nil)
Expand Down