shaps80 / SwiftUIBackports

A collection of SwiftUI backports for iOS, macOS, tvOS and watchOS
MIT License
932 stars 59 forks source link

PhotoPicker not binding data #51

Open zjinhu opened 1 year ago

zjinhu commented 1 year ago

PhotoPicker not binding data

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

shaps80 commented 1 year ago

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"?

zjinhu commented 1 year ago

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

shaps80 commented 1 year ago

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/

zjinhu commented 1 year ago

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 commented 1 year ago

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

shaps80 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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.

X901 commented 11 months ago

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

shaps80 commented 11 months ago

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.

zjinhu commented 10 months ago

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

X901 commented 10 months ago

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:)