aboutsummaryrefslogtreecommitdiff
path: root/Demo/SwiftyCropDemo/ContentView.swift
blob: f4081a2362112b003bb9390f88f59a7f0ebcaed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import SwiftUI
import SwiftyCrop

struct ContentView: View {
    @State private var showImageCropper: Bool = false
    @State private var selectedImage: UIImage?
    @State private var selectedShape: MaskShape = .square
    @State private var cropImageCircular: Bool
    @State private var rotateImage: Bool
    @State private var maxMagnificationScale: CGFloat
    @State private var maskRadius: CGFloat
    @FocusState private var textFieldFocused: Bool
    
    init() {
        let defaultConfiguration = SwiftyCropConfiguration()
        _cropImageCircular = State(initialValue: defaultConfiguration.cropImageCircular)
        _rotateImage = State(initialValue: defaultConfiguration.rotateImage)
        _maxMagnificationScale = State(initialValue: defaultConfiguration.maxMagnificationScale)
        _maskRadius = State(initialValue: defaultConfiguration.maskRadius)
    }
    
    var body: some View {
        VStack {
            Spacer()
            
            Group {
                if let selectedImage = selectedImage {
                    Image(uiImage: selectedImage)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .cornerRadius(8)
                } else {
                    ProgressView()
                }
            }
            .scaledToFit()
            .padding()
            
            Spacer()
            
            GroupBox {
                VStack(spacing: 15) {
                    HStack {
                        Button {
                            loadImage()
                        } label: {
                            LongText(title: "Load image")
                        }
                        Button {
                            showImageCropper.toggle()
                        } label: {
                            LongText(title: "Crop image")
                        }
                    }
                    
                    HStack {
                        Text("Mask shape")
                            .frame(maxWidth: .infinity, alignment: .leading)
                        
                        Picker("maskShape", selection: $selectedShape) {
                            ForEach(MaskShape.allCases, id: \.self) { mask in
                                Text(String(describing: mask))
                            }
                        }
                        .pickerStyle(.segmented)
                    }
                    
                    Toggle("Crop image to circle", isOn: $cropImageCircular)
                    
                    Toggle("Rotate image", isOn: $rotateImage)
                    
                    HStack {
                        Text("Max magnification")
                            .frame(maxWidth: .infinity, alignment: .leading)
                        
                        DecimalTextField(value: $maxMagnificationScale)
                            .focused($textFieldFocused)
                    }
                    
                    HStack {
                        Text("Mask radius")
                            .frame(maxWidth: .infinity, alignment: .leading)
                        
                        Button {
                            maskRadius = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) / 2
                        } label: {
                            Image(systemName: "arrow.up.left.and.arrow.down.right")
                                .font(.footnote)
                        }
                        
                        DecimalTextField(value: $maskRadius)
                            .focused($textFieldFocused)
                    }
                }
            }
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    
                    Button("Done") {
                        textFieldFocused = false
                    }
                }
            }
            .buttonStyle(.bordered)
            .padding()
        }
        .onAppear {
            loadImage()
        }
        .fullScreenCover(isPresented: $showImageCropper) {
            if let selectedImage = selectedImage {
                SwiftyCropView(
                    imageToCrop: selectedImage,
                    maskShape: selectedShape,
                    configuration: SwiftyCropConfiguration(
                        maxMagnificationScale: maxMagnificationScale,
                        maskRadius: maskRadius,
                        cropImageCircular: cropImageCircular,
                        rotateImage: rotateImage
                    )
                ) { croppedImage in
                    // Do something with the returned, cropped image
                    self.selectedImage = croppedImage
                }
            }
        }
    }
    
    private func loadImage() {
        Task {
            selectedImage = await downloadExampleImage()
        }
    }
    
    // Example function for downloading an image
    private func downloadExampleImage() async -> UIImage? {
        let portraitUrlString = "https://picsum.photos/1000/1200"
        let landscapeUrlString = "https://picsum.photos/2000/1000"
        let urlString = Int.random(in: 0...1) == 0 ? portraitUrlString : landscapeUrlString
        guard let url = URL(string: urlString),
              let (data, _) = try? await URLSession.shared.data(from: url),
              let image = UIImage(data: data)
        else { return nil }
        
        return image
    }
}

#Preview {
    ContentView()
}