Fotobibliothek mit Swift UI

Zeigen Sie Ihre Fotobibliothek in der Swift-Benutzeroberfläche an

Tippen Sie auf die Schaltfläche, um die Fotobibliothek anzuzeigen. Wählen Sie ein Foto in der Fotobibliothek aus, um es im Vollbildmodus anzuzeigen.

Umgebung

Swift 5.3 Xcode 12.0.1 macOS 10.15.7

Fotobibliothek

Definiert eine Struktur, die die Fotobibliothek in SwiftUI umschließt.

ImagePicker.swift


import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    var sourceType: UIImagePickerController.SourceType = .photoLibrary
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let imagePicker = UIImagePickerController()
        imagePicker.allowsEditing = false
        imagePicker.sourceType = sourceType
        return imagePicker
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {

    }
}

Erster Bildschirm

Dies ist die grundlegende Bildschirmstruktur der App. Zeigt die Fotobibliothek im Blattformat basierend auf Statusvariablen an.

ContentView.swift


import SwiftUI

struct ContentView: View {
    @State private var image = UIImage()
    @State private var isShowPhotoLibrary = false
    
    var body: some View {
        VStack {
            Image(uiImage: self.image)
            Button(action: {
                self.isShowPhotoLibrary = true
            }, label: {
                Text("Photo Library")
                    .padding()
            })
        }
        .sheet(isPresented: $isShowPhotoLibrary, content: {
            ImagePicker(sourceType: .photoLibrary)
        })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Wählen Sie ein Foto in Ihrer Fotobibliothek aus

Entspricht dem Protokoll "UIImagePickerControllerDelegate".

ImagePicker.swift


import SwiftUI

struct ImagePicker: UIViewControllerRepresentable {
    // MARK: - Working with UIViewControllerRepresentable
    var sourceType: UIImagePickerController.SourceType = .photoLibrary
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let imagePicker = UIImagePickerController()
        imagePicker.allowsEditing = false
        imagePicker.sourceType = sourceType
        imagePicker.delegate = context.coordinator  // Coordinater to adopt UIImagePickerControllerDelegate Protcol.
        return imagePicker
    }
    
    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {

    }

    // MARK: - Using Coordinator to Adopt the UIImagePickerControllerDelegate Protocol
    @Binding var selectedImage: UIImage
    @Environment(\.presentationMode) private var presentationMode
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        var parent: ImagePicker
        
        init(_ parent: ImagePicker) {
            self.parent = parent
        }
     
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
     
            if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
                parent.selectedImage = image
            }
     
            parent.presentationMode.wrappedValue.dismiss()
        }
    }
}

Ausgewählte Fotos anzeigen

Fügen Sie der Ansicht "Bild" einen Modifikator hinzu. Stellen Sie das ausgewählte Foto zum Zeitpunkt des Bildschirmübergangs auf die Eigenschaft "Bild" ein.

ContentView.swift


struct ContentView: View {
    @State private var image = UIImage()
    @State private var isShowPhotoLibrary = false
    
    var body: some View {
        VStack {
            Image(uiImage: self.image)
                .resizable()
                .scaledToFill()
                .frame(minWidth: 0, maxWidth: .infinity)
                .edgesIgnoringSafeArea(.all)
            Button(action: {
                self.isShowPhotoLibrary = true
            }, label: {
                Text("Photo Library")
                    .padding()
            })
        }
        .sheet(isPresented: $isShowPhotoLibrary, content: {
            ImagePicker(sourceType: .photoLibrary, selectedImage: self.$image)
        })
    }
}

Recommended Posts

Fotobibliothek mit Swift UI
Implementierung von Seitenmenüs in der Swift-Benutzeroberfläche
Schnelle UI 100 klopft
Passen Sie die Ansicht mit dem Ansichtsmodifikator in der Swift-Benutzeroberfläche an
Implementierungsnotiz für SKStoreReviewController in der Swift-Benutzeroberfläche von iOS14
Teilen wird in Swift zu 0
Beginnend mit Swift Swift UI
Mehrdimensionales Array in Swift
Liste der Geräte, die mit Swift UI in der Vorschau angezeigt werden können
So ändern Sie die Hintergrundfarbe der Navigationsleiste usw. in der Swift-Benutzeroberfläche
Machen Sie mit Swift einen FPS-Zähler
Automatische Methode zur Größenänderung von Fotos von Java
[Native reagieren] Schreiben Sie ein natives Modul in Swift
Geben Sie in Swift mehrere Sortierbedingungen an
Behandle C char ** gut mit Swift
Auch bei Swift Zeitstempel in Nanosekunden!
Versuchen Sie, mit UIViewRepresentable eine leicht reichhaltige Webansicht in SwiftUI anzuzeigen