SwiftUIBackports icon indicating copy to clipboard operation
SwiftUIBackports copied to clipboard

PhotoPicker not binding data

Open zjinhu opened this issue 1 year ago • 12 comments

PhotoPicker not binding data

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { isPresented.wrappedValue = false }

zjinhu avatar Jun 08 '23 03:06 zjinhu

This is extremely unclear. I have no idea what you're suggesting or referencing here I'm afraid. Please add a lot more detail so I can understand the "issue"?

shaps80 avatar Jun 13 '23 08:06 shaps80

This is extremely unclear. I have no idea what you're suggesting or referencing here I'm afraid. Please add a lot more detail so I can understand the "issue"?

How to get the corresponding Photo or Video Data through PhotoPicker, there is no corresponding data binding here

zjinhu avatar Jun 14 '23 03:06 zjinhu

I see, in your first comment you referenced internal details which is not relevant to your question.

The original API is completion based, so obvs you don't see the binding being setup there. But also that's internal and you cannot use that anyway.

Rather you want to use the modifier on your SwiftUI view similar to the following:

struct PhotosSelector: View {
    @State var selectedItems: [PhotosPickerItem] = []


    var body: some View {
        Backport.PhotosPicker(
            selection: $selectedItems,
            matching: .images
        ) {
            Text("Select Multiple Photos")
        }
    }
}

You can also use selection: Backport.PhotoPickerItem? to do single selection.

PhotosPicker (official and backport) are essentially just wrappers around a Button. So when a user taps this view, the picker is presented and the selected items are provided via the binding you pass in.

FYI all of my API's are 100% identical to the official versions, so looking at the official docs can also be helpful: https://developer.apple.com/documentation/photokit/photospicker/

shaps80 avatar Jun 15 '23 13:06 shaps80

struct PhotosDemoView: View {

@State private var selection: [Backport<Any>.PhotosPickerItem] = []

@State var image: UIImage?

var body: some View {
    List {

        Backport.PhotosPicker(selection: $selection,
                              maxSelectionCount: 3,
                              matching: .any(of: [.images])) {
            Label("Choose Photos", systemImage: "photo")
        }
        
        if let image {
            Image(uiImage: image)
        }
        
    }
    .navigationTitle("Photos Picker")
    .onChange(of: selection) { _ in
        Task{
            if let item = selection.first,
               let date = try? await item.loadTransferable(type: Data.self) {
                image = UIImage(data: date)
            }
        }
    }
}

}

selection is no changes, is not work

zjinhu avatar Jun 16 '23 03:06 zjinhu

I guess the photo data selected by PHPickerViewController is not binding to PhotosPickerItem

zjinhu avatar Jun 16 '23 03:06 zjinhu

Hmmm this was implemented some time ago, I'm kinda surprised if this isn't working and it's only being reported now. I guess no one is using it, but I'm not 100%. I'll need to re-test and work out what's going on – but I do not have time atm so this may take a couple weeks I'm afraid.

shaps80 avatar Jun 16 '23 12:06 shaps80

@jackiehu can you confirm what iOS version you're testing against? There are 2 implementations included, one for iOS 13 and the other for iOS 14+

shaps80 avatar Jun 16 '23 13:06 shaps80

Ouch, I apologise but I think a WIP was accidentally merged into main and then released prematurely.

I will pull this immediately as this is not at all complete. All the API pieces are there, but a lot of the implementation still has fatalError() for example as placeholders.

I will need to revisit this and re-release at a later date, I'm sorry I have no idea how this happened. My bad 🤦‍♂️


EDIT: I've now pushed a new release that removes/cleans this up for now. I will try and complete this asap and get it into an upcoming release, sorry about that.

shaps80 avatar Jun 16 '23 13:06 shaps80

@shaps80 Hi Shaps, will we see new update for PhotosPicker soon ?

X901 avatar Aug 08 '23 09:08 X901

Not at least for a couple of weeks. I'm working 2 other contracts atm and then next week I'm on holiday for a week. So Sep is the earliest I'll have a chance to revisit this sorry.

shaps80 avatar Aug 08 '23 21:08 shaps80

My copy,and use now https://github.com/jackiehu/Brick_SwiftUI/tree/main/Sources/Tools/PhotoPicker

zjinhu avatar Aug 09 '23 02:08 zjinhu

Not at least for a couple of weeks. I'm working 2 other contracts atm and then next week I'm on holiday for a week. So Sep is the earliest I'll have a chance to revisit this sorry.

No problem, take your time

My copy,and use now https://github.com/jackiehu/Brick_SwiftUI/tree/main/Sources/Tools/PhotoPicker

Awesome, I tried it it working find with a little bit of change to my code to support version below iOS 16 I change PhotosPicker to PhotoPicker, PhotosPickerItem to PHPickerResult and loadTransferable(type:) to loadTransfer(type:)

X901 avatar Aug 09 '23 14:08 X901