@@ -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}
0 commit comments