PhotosPicker is not working properly

Hello!

I want to use PhotosPicker, but I'm encountering an error with the line item.loadTransferable(type: Data.self) which says Instance method 'loadTransferable(type:)' requires that 'Data' conform to 'Transferable'. I cannot execute the code because of this error. Why is this happening? Any information would be helpful.

I tried changing type: Data.self to type: UIImage.self, but it still didn’t work.

I am using Xcode 15.4, and my target version is iOS 17.4.

import SwiftUI
import PhotosUI

struct PhotosPicker: View {

    @State var selectedItems: [PhotosPickerItem] = []

    @State var selectedImages: [UIImage] = []

    var body: some View {

        VStack {
        
            if !selectedImages.isEmpty {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack {
                        ForEach(selectedImages, id: \.self) { image in
                            Image(uiImage: image)
                                .resizable()
                                .scaledToFill()
                                .frame(width: 300, height: 200)
                        }
                    }
                }
            }
            
            PhotosPicker(
                selection: $selectedItems,
                selectionBehavior: .ordered,
                matching: .images
            ) {
                Text("image!")
            }
        }
        .onChange(of: selectedItems) { items in

            
            Task {
                selectedImages = []

                for item in items {
                    guard let data = try await item.loadTransferable(type: Data.self) else { continue }
                    guard let uiImage = UIImage(data: data) else { continue }
                    selectedImages.append(uiImage)
                }
            }
        }
    }
}
Answered by DTS Engineer in 799782022

@max_us Bringing Photos picker to your SwiftUI app Sample project covers how you could retrieve a SwiftUI image by using Transferable.

I cannot reproduce the Data error you are seeing. However, Xcode is complaining Argument passed to call that takes no arguments on your PhotosPicker( selection: $selectedItems, selectionBehavior: .ordered, matching: .images ) { Text("image!") } code. That is because you named your struct PhotosPicker as well, which is confusing the compiler.

I changed it to struct MyPhotosPicker: View {...}, but the error remains the same. Instance method 'loadTransferable(type:)' requires that 'Data' conform to 'Transferable'

Is it normal to convert to the Data type each time? I felt that it might be more efficient to convert to the Data type when saving.

If you have error-free code for selecting multiple photos with PhotosPicker and displaying them on the screen, I would appreciate it. Even official examples from WWDC would be great.

@max_us Bringing Photos picker to your SwiftUI app Sample project covers how you could retrieve a SwiftUI image by using Transferable.

PhotosPicker is not working properly
 
 
Q