Why does the first image behave differently ?

I have the following test code:

import SwiftUI

struct ContentView: View {
    @State private var draggedImages: [UIImage?] = Array(repeating: nil, count: 5)
    @State private var savedToDisk = false
    
    var body: some View {
        VStack {
            HStack {
                ForEach(0..<5, id: \.self) { index in
                    let image = draggedImages[index] ?? UIImage(systemName: "photo")
                    
                    Image(uiImage: image!)
                        .resizable()
                        .frame(width: 80, height: 80)
                        .padding()
                        .background(Color.blue)
                        .foregroundColor(.white)
                        .cornerRadius(10)
                        .onDrag {
                            return NSItemProvider(object: UIImage(systemName: "photo")!)
                        }
                        .onDrop(of: ["public.image"], isTargeted: nil) { providers, _ in
                            providers.first?.loadDataRepresentation(forTypeIdentifier: "public.image") { data, error in
                                if let data = data, let uiImage = UIImage(data: data) {
                                    draggedImages[index] = uiImage
                                    saveImageToDisk(image: uiImage, imageNumber: index)
                                }
                            }
                            return true
                        }
                }
                
                .alert(isPresented: $savedToDisk) {
                    Alert(title: Text("Images Saved"), message: Text("The images have been saved to disk."), dismissButton: .default(Text("OK")))
                }
                
                Spacer()
            }
        }
        .padding()
    }
    
    private func saveImageToDisk(image: UIImage, imageNumber: Int) {
        if let imageData = image.pngData() {
            do {
                let documentsDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
                let imageUrl = documentsDirectory.appendingPathComponent("image_\(imageNumber).png")
                try imageData.write(to: imageUrl)
                savedToDisk = true
            } catch {
                print("Error saving image: \(error)")
            }
        }
    }
}

I can drag and drop as well as the dragged image is save on the second placeholder thru the fifth but not the first When I drag and drop to the first image another files app opens but when I close this the image is not saved. Any Ideas why the first image behaves differently to all the others?

I test on Sonoma and Xcode 15 beta 7, I wil give it a try on Ventura and Xcode 14 later today to see if the same thing happens. Thanks

Answered by BigEagle in 765278022

This is fixed in the Release Candidate code.

Could you check providers is not empty (for whatever reason) when index == 0 ?

                        .onDrop(of: ["public.image"], isTargeted: nil) { providers, _ in
                            print("providers", providers.count, providers)
                            providers.first?.loadDataRepresentation(forTypeIdentifier: "public.image") { data, error in
                                if let data = data, let uiImage = UIImage(data: data) {
                                    draggedImages[index] = uiImage
                                    saveImageToDisk(image: uiImage, imageNumber: index)
                                }
                            }
                            return true
                        }

I tested (Xcode 14.2) drag and drop work (just had to remember you need to press for some time).

So your problem is for saving ? Do you get bthe message: print("Error saving image: (error)") ?

I don't know on which device or simulator you test.

Just try reducing size, in case it is a problem of image position on screen (but you should probably have the same issue with 5th image)

                        .frame(width: 40, height: 40)

I have my iPad connected to my Mac and I am running the code above on the iPad. I am using my iPad to test because it has more images to test with than a simulator. This code works as expected on my Mac but not on my iPad. The specifics of all the devices are as follows: Mac Sonoma beta 6 Xcode 15 beta7, iPadOS 17 beta 7.

Reducing the frame size makes no difference.

I will test on Ventura with Xcode 14 and iPadOS 16 later today or in the morning to see if this makes a difference.

Thanks for the help!

Does the same thing on Ventura and Xcode 14

Accepted Answer

This is fixed in the Release Candidate code.

Why does the first image behave differently ?
 
 
Q