Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI - Initial Selection from UIImagePickerController Does Not Appear #328

Open
test-ArG5R4aR78 opened this issue Mar 14, 2021 · 3 comments · May be fixed by #330
Open

SwiftUI - Initial Selection from UIImagePickerController Does Not Appear #328

test-ArG5R4aR78 opened this issue Mar 14, 2021 · 3 comments · May be fixed by #330

Comments

@test-ArG5R4aR78
Copy link

test-ArG5R4aR78 commented Mar 14, 2021

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)")
        }
    }
}
@test-ArG5R4aR78 test-ArG5R4aR78 changed the title SwiftUI - Initial Selection from ImagePickerController Does Not Appear SwiftUI - Initial Selection from UIImagePickerController Does Not Appear Mar 14, 2021
@mikaoj
Copy link
Owner

mikaoj commented Mar 14, 2021

If you are using the delegate and presenting the image picker yourself you also need to request permission to the photo library before presenting the picker.

Similar discussion here: #319

@AntonClaesson
Copy link

I have the same issue. Any update on this?

@Taimoor20
Copy link

@AntonClaesson did you find any solution for this. I am stuck now at this point for more than 3 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants