Skip to content

SwiftUI - Initial Selection from UIImagePickerController Does Not Appear  #328

@test-ArG5R4aR78

Description

@test-ArG5R4aR78

Hello,

Only for the initial render, BSImagePicker does not seem to display the images that Core UIKit UIImagePickerController has provided access to. Re-opening the BSImagePicker does however display them.

I've included the SwiftUI code and a .mov file showing this behavior. How can developers make the initial display of the selected images identical to the behavior when BSImagePicker is re-opened?

I am using 750f26a git ref. Tag 3.3.1.

Thank you for your help, and thank you for developing this piece of software.

BSImagePicker_Issue.mov
struct ContentView: View {
    @StateObject var imagePickerCoordinatorView = ImagePickerCoordinatorView()
    @State var isPresentedImagePicker = false
    
    var body: some View {
        Button("Open BSImagePicker") {
            self.isPresentedImagePicker = true
        }
        .sheet(isPresented: self.$isPresentedImagePicker, content: {
            imagePickerCoordinatorView
        })
        
        if let list = imagePickerCoordinatorView.selectedAssets {
            Text("\(list.count) images selected")
        } else {
            Text("No images selected")
        }
    }
}
//
//  ImagePickerCoordinatorView.swift

import SwiftUI
import Photos

import BSImagePicker


final class ImagePickerCoordinatorView: NSObject, ObservableObject {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    
    @Published var selectedAssets: [PHAsset]? {
        didSet {
            print("New value of selectedAssets is \(selectedAssets)")
        }
    }
    
    func dismiss() {
        self.presentationMode.wrappedValue.dismiss()
    }
    
    func clearSelectedAssets() {
        self.selectedAssets = [PHAsset]()
    }
}

extension ImagePickerCoordinatorView: UIViewControllerRepresentable {
    
    public typealias UIViewControllerType = ImagePickerController
    
    public func makeUIViewController(context: Context) -> ImagePickerController {
        let picker = ImagePickerController()
        picker.settings.selection.max = 10
        picker.settings.selection.unselectOnReachingMax = false
        picker.settings.theme.selectionStyle = .numbered
        picker.settings.fetch.assets.supportedMediaTypes = [.image]
        picker.imagePickerDelegate = context.coordinator
        return picker
    }
    
    public func updateUIViewController(_ uiViewController: ImagePickerController, context: Context) {
    }
    
    public func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
}

extension ImagePickerCoordinatorView {
    public class Coordinator: ImagePickerControllerDelegate {
        private let parent: ImagePickerCoordinatorView
        
        public init(_ parent: ImagePickerCoordinatorView) {
            self.parent = parent
        }
        
        public func imagePicker(_ imagePicker: ImagePickerController, didSelectAsset asset: PHAsset) {
            print("Selected: \(asset)")
        }
        
        public func imagePicker(_ imagePicker: ImagePickerController, didDeselectAsset asset: PHAsset) {
            print("Deselected: \(asset)")
        }
        
        public func imagePicker(_ imagePicker: ImagePickerController, didFinishWithAssets assets: [PHAsset]) {
            print("Finished with selections: \(assets)")
            parent.selectedAssets = assets
            parent.dismiss()
        }
        
        public func imagePicker(_ imagePicker: ImagePickerController, didCancelWithAssets assets: [PHAsset]) {
            print("Canceled with selections: \(assets)")
            if (assets.count == 0) {
                print("Canceled with no selection so setting selectedAssets to empty")
                parent.selectedAssets?.removeAll()
            }
            parent.dismiss()
        }
        
        public func imagePicker(_ imagePicker: ImagePickerController, didReachSelectionLimit count: Int) {
            print("Did Reach Selection Limit: \(count)")
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions