-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screenshot.swift
51 lines (44 loc) · 1.34 KB
/
Screenshot.swift
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
import SwiftUI
struct Screenshot: View {
@State private var image: UIImage? = nil
var body: some View {
VStack {
Button(action: {
self.getScreenshot()
}) {
Text("Take Screenshot")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding()
if let image = image {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
.padding()
}
}
}
func getScreenshot() {
guard let url = URL(string: "\(Constants.serverIP)/screenshot") else { return }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
print("Error: \(error?.localizedDescription ?? "Unknown error")")
return
}
if let image = UIImage(data: data) {
DispatchQueue.main.async {
self.image = image
}
}
}
task.resume()
}
}
struct Screenshot_Previews: PreviewProvider {
static var previews: some View {
Screenshot()
}
}