Tippen Sie auf die Schaltfläche, um die Fotobibliothek anzuzeigen. Wählen Sie ein Foto in der Fotobibliothek aus, um es im Vollbildmodus anzuzeigen.
Swift 5.3 Xcode 12.0.1 macOS 10.15.7
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>) {
}
}
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()
}
}
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()
}
}
}
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