Home Swift UNIX C Assembly Go Web MCU Research Non-Tech

How to get images from the system album in SwiftUI

2021-12-10 | Swift | #Words: 608 | 中文原版

SwiftUI cannot obtain the system album content by itself, so it has to rely on UIKit. The header file is as follows:

import SwiftUI
importUIKit

First we create an ImagePicker structure for us to obtain the system album content:

struct ImagePicker: UIViewControllerRepresentable {
     @Environment(\.presentationMode) private var presentationMode
     let sourceType: UIImagePickerController.SourceType
     let onImagePicked: (UIImage) -> Void
 
     final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
 
         @Binding private var presentationMode: PresentationMode
         private let sourceType: UIImagePickerController.SourceType
         private let onImagePicked: (UIImage) -> Void
 
         init(presentationMode: Binding<PresentationMode>,
              sourceType: UIImagePickerController.SourceType,
              onImagePicked: @escaping (UIImage) -> Void) {
             _presentationMode = presentationMode
             self.sourceType = sourceType
             self.onImagePicked = onImagePicked
         }
 
         func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
             let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
             onImagePicked(uiImage)
             presentationMode.dismiss()
         }
 
         func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
             presentationMode.dismiss()
         }
 
     }
 
     func makeCoordinator() -> Coordinator {
         return Coordinator(presentationMode: presentationMode,
                            sourceType: sourceType,
                            onImagePicked: onImagePicked)
     }
 
     func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
         let picker = UIImagePickerController()
         picker.sourceType = sourceType
         picker.delegate = context.coordinator
         return picker
     }
 
     func updateUIViewController(_ uiViewController: UIImagePickerController,
                                 context: UIViewControllerRepresentableContext<ImagePicker>) {
     }
}

Then we can easily get the photos from the system album:

struct ContentView: View {
     @State private var showImagePicker = false
     //The image here is used to place the photos that will be obtained later.
     @State private var image: UIImage = UIImage()
     var body: some View {
         List{
             Button(action: {
                 showImagePicker = true
             }, label: {
                 Text("Select Image")
             })
            
             Image(uiImage: image)
                 .resizable()
                 .aspectRatio(contentMode: .fit)
         }
         .sheet(isPresented: $showImagePicker,
                content: {
             ImagePicker(sourceType: .photoLibrary) { image in
                 self.image = image
             }
         })
     }
}

However, this can only obtain one photo at a time, but we can still obtain it.

I hope these will help someone in need~