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

PhotoPicker not binding data #51

Open
zjinhu opened this issue Jun 8, 2023 · 12 comments
Open

PhotoPicker not binding data #51

zjinhu opened this issue Jun 8, 2023 · 12 comments
Labels
bug Something isn't working

Comments

@zjinhu
Copy link

zjinhu commented Jun 8, 2023

PhotoPicker not binding data

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

@zjinhu zjinhu added the bug Something isn't working label Jun 8, 2023
@shaps80
Copy link
Owner

shaps80 commented Jun 13, 2023

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
Copy link
Author

zjinhu commented Jun 14, 2023

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
Copy link
Owner

shaps80 commented Jun 15, 2023

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
Copy link
Author

zjinhu commented Jun 16, 2023

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
Copy link
Author

zjinhu commented Jun 16, 2023

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

@shaps80
Copy link
Owner

shaps80 commented Jun 16, 2023

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
Copy link
Owner

shaps80 commented Jun 16, 2023

@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
Copy link
Owner

shaps80 commented Jun 16, 2023

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
Copy link

X901 commented Aug 8, 2023

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

@shaps80
Copy link
Owner

shaps80 commented Aug 8, 2023

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
Copy link
Author

zjinhu commented Aug 9, 2023

@X901
Copy link

X901 commented Aug 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants