-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageFileView.swift
56 lines (51 loc) · 1.52 KB
/
ImageFileView.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
51
52
53
54
55
56
//
// ImageFileView.swift
//
// Created by Speedyfriend67 on 27.06.24
//
import SwiftUI
struct ImageFileView: View {
let fileURL: URL
@State private var image: Image?
@State private var isLoading: Bool = true
@State private var showError: Bool = false
var body: some View {
VStack {
if isLoading {
ProgressView("Loading...")
.padding()
} else if showError {
Text("Failed to load image.")
.foregroundColor(.red)
} else {
image?
.resizable()
.aspectRatio(contentMode: .fit)
.padding()
}
}
.onAppear(perform: loadImage)
.navigationTitle(fileURL.lastPathComponent)
.navigationBarTitleDisplayMode(.inline)
}
private func loadImage() {
DispatchQueue.global(qos: .userInitiated).async {
if let uiImage = UIImage(contentsOfFile: fileURL.path) {
DispatchQueue.main.async {
self.image = Image(uiImage: uiImage)
self.isLoading = false
}
} else {
DispatchQueue.main.async {
self.isLoading = false
self.showError = true
}
}
}
}
}
struct ImageFileView_Previews: PreviewProvider {
static var previews: some View {
ImageFileView(fileURL: URL(fileURLWithPath: "/var/tmp/sample.png"))
}
}