aboutsummaryrefslogtreecommitdiff
path: root/Demo/SwiftyCropDemo/ContentView.swift
blob: 235a8628e38c3bc9ce07844dbe18f28d4884cb3c (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
//
//  ContentView.swift
//  SwiftyCropDemo
//
//  Created by Leonid Zolotarev on 1/23/24.
//

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 = false
    @State private var rotateImage: Bool = true

    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 {
                HStack {
                    Button {
                        loadImage()
                    } label: {
                        LongText(title: "Load image")
                    }
                    Button {
                        showImageCropper.toggle()
                    } label: {
                        LongText(title: "Crop image")
                    }
                }
                HStack {
                    ShapeButton(
                        title: "Use square crop",
                        shape: .square,
                        selection: $selectedShape
                    )
                    ShapeButton(
                        title: "Use circle crop",
                        shape: .circle,
                        selection: $selectedShape
                    )
                }
                Toggle("Crop image to circle", isOn: $cropImageCircular)
            }
            .buttonStyle(.bordered)
            .padding()
        }
        .onAppear {
            loadImage()
        }
        .fullScreenCover(isPresented: $showImageCropper) {
            if let selectedImage = selectedImage {
                SwiftyCropView(
                    imageToCrop: selectedImage,
                    maskShape: selectedShape,
                    configuration: SwiftyCropConfiguration(
                        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 urlString = "https://picsum.photos/1000/1200"
        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()
}