aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorleoz <leoz@yahoo.com>2024-02-25 09:58:40 -0500
committerleoz <leoz@yahoo.com>2024-02-25 09:58:40 -0500
commit7eb8df7f203857748d84f325dd82a443326f174b (patch)
tree7bdf934f1a3948bb15dce44a0283bd25777b3700
parent9301acdc650cf6b06465ef0fb90c6451ecbf4444 (diff)
Use configuration for image rotation
-rw-r--r--Demo/SwiftyCropDemo/ContentView.swift4
-rw-r--r--Sources/SwiftyCrop/View/CropView.swift20
2 files changed, 15 insertions, 9 deletions
diff --git a/Demo/SwiftyCropDemo/ContentView.swift b/Demo/SwiftyCropDemo/ContentView.swift
index c999dd3..235a862 100644
--- a/Demo/SwiftyCropDemo/ContentView.swift
+++ b/Demo/SwiftyCropDemo/ContentView.swift
@@ -13,6 +13,7 @@ struct ContentView: View {
@State private var selectedImage: UIImage?
@State private var selectedShape: MaskShape = .square
@State private var cropImageCircular: Bool = false
+ @State private var rotateImage: Bool = true
var body: some View {
VStack {
@@ -69,7 +70,8 @@ struct ContentView: View {
imageToCrop: selectedImage,
maskShape: selectedShape,
configuration: SwiftyCropConfiguration(
- cropImageCircular: cropImageCircular
+ cropImageCircular: cropImageCircular,
+ rotateImage: rotateImage
)
) { croppedImage in
// Do something with the returned, cropped image
diff --git a/Sources/SwiftyCrop/View/CropView.swift b/Sources/SwiftyCrop/View/CropView.swift
index 9619efb..a61aa47 100644
--- a/Sources/SwiftyCrop/View/CropView.swift
+++ b/Sources/SwiftyCrop/View/CropView.swift
@@ -111,7 +111,7 @@ struct CropView: View {
.frame(maxWidth: .infinity, maxHeight: .infinity)
.simultaneousGesture(magnificationGesture)
.simultaneousGesture(dragGesture)
- .simultaneousGesture(rotationGesture)
+ .simultaneousGesture(configuration.rotateImage ? rotationGesture : nil)
HStack {
Button {
@@ -138,15 +138,19 @@ struct CropView: View {
}
private func cropImage() -> UIImage? {
- if let rotatedImage: UIImage = viewModel.rotate(image, viewModel.lastAngle) {
- if maskShape == .circle && configuration.cropImageCircular {
- return viewModel.cropToCircle(rotatedImage)
- } else {
- return viewModel.cropToSquare(rotatedImage)
+ var editedImage: UIImage = image
+ if configuration.rotateImage {
+ if let rotatedImage: UIImage = viewModel.rotate(
+ editedImage,
+ viewModel.lastAngle
+ ) {
+ editedImage = rotatedImage
}
- return rotatedImage
+ }
+ if configuration.cropImageCircular && maskShape == .circle {
+ return viewModel.cropToCircle(editedImage)
} else {
- return nil
+ return viewModel.cropToSquare(editedImage)
}
}