@@ -36,6 +36,12 @@ class HybridImage: HybridImageSpec {
3636 }
3737
3838 func resize( width: Double , height: Double ) throws -> any HybridImageSpec {
39+ guard width > 0 else {
40+ throw RuntimeError . error ( withMessage: " Width cannot be less than 0! (width: \( width) ) " )
41+ }
42+ guard height > 0 else {
43+ throw RuntimeError . error ( withMessage: " Height cannot be less than 0! (height: \( height) ) " )
44+ }
3945 let targetSize = CGSize ( width: width, height: height)
4046
4147 let renderer = UIGraphicsImageRenderer ( size: targetSize)
@@ -52,6 +58,34 @@ class HybridImage: HybridImageSpec {
5258 }
5359 }
5460
61+ func crop( startX: Double , startY: Double , endX: Double , endY: Double ) throws -> any HybridImageSpec {
62+ let targetWidth = endX - startX
63+ let targetHeight = endY - startY
64+ guard targetWidth > 0 else {
65+ throw RuntimeError . error ( withMessage: " Width cannot be less than 0! (startX: \( startX) - endX: \( endX) = \( width) ) " )
66+ }
67+ guard targetHeight > 0 else {
68+ throw RuntimeError . error ( withMessage: " Height cannot be less than 0! (startY: \( startY) - endY: \( endY) = \( height) ) " )
69+ }
70+ guard let cgImage = uiImage. cgImage else {
71+ throw RuntimeError . error ( withMessage: " This image does not have an underlying .cgImage! " )
72+ }
73+
74+ let targetRect = CGRect ( origin: CGPoint ( x: startX, y: startY) ,
75+ size: CGSize ( width: width, height: height) )
76+ guard let croppedCgImage = cgImage. cropping ( to: targetRect) else {
77+ throw RuntimeError . error ( withMessage: " Failed to crop CGImage to \( targetRect) ! " )
78+ }
79+ let croppedUiImage = UIImage ( cgImage: croppedCgImage)
80+ return HybridImage ( uiImage: croppedUiImage)
81+ }
82+
83+ func cropAsync( startX: Double , startY: Double , endX: Double , endY: Double ) throws -> Promise < any HybridImageSpec > {
84+ return Promise . async {
85+ return try self . crop ( startX: startX, startY: startY, endX: endX, endY: endY)
86+ }
87+ }
88+
5589 private func saveImage( to path: String , format: ImageFormat , quality: Double ) throws {
5690 guard let data = uiImage. getData ( in: format, quality: quality) else {
5791 throw RuntimeError . error ( withMessage: " Failed to get Image data in format \( format. stringValue) with quality \( quality) ! " )
0 commit comments