UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Error when saving image to Documents Directory

Forums > 100 Days of SwiftUI

I am still working on the app, but I have run into a problem I don't understand. I decided to save the images and information as a combination of a json encoded file and then related UUID identified image files in the document directory. Periodically when saving the image I get the following error:

1) Here's debug I have in my app:

2022-12-30 11:51:38.673804-0500 Challenge77[9990:3528132] 
[claims] 27328842-F382-4DBF-BE78-27B42569217E 
grantAccessClaim reply is an error: Error Domain=NSCocoaErrorDomain Code=4101 
"Couldn’t communicate with a helper application."
UserInfo={NSUnderlyingError=0x600002f29fb0 {Error Domain=PHAssetExportRequestErrorDomain Code=0 
"The operation couldn’tbe completed. (PHAssetExportRequestErrorDomain error 0.)" 
UserInfo={NSLocalizedDescription=The operation couldn’t becompleted. (PHAssetExportRequestErrorDomain error 0.),
NSUnderlyingError=0x600002f2a760 {Error Domain=PAMediaConversionServiceErrorDomain Code=2 
"The operation couldn’t be completed.(PAMediaConversionServiceErrorDomain error 2.)" 
UserInfo={NSLocalizedDescription=The operation couldn’t be completed.
(PAMediaConversionServiceErrorDomain error 2.)}}}}}

Any suggestions on what I am doing wrong?

My code can be found on github at - https://github.com/TheApApp/Challenge77

(formatted error to make it easier to read)

2      

Note, if I hit the home button on the simulator and go back in, I can save the next image no problem. Also, when this error happens, the Add button becomes disabled until I hit the home button. I assume the app is in some kinda of hung state.

2      

It appears that this is a thread issue in my ImagePicker. The warning shows up as

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

in the following function:

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            picker.dismiss(animated: true)  // let's get rid of the picker

            guard let provider = results.first?.itemProvider else { return } // let's see if there is anything in the provider if not bail out

            if provider.canLoadObject(ofClass: UIImage.self) {
                provider.loadObject(ofClass: UIImage.self ) { image, _ in
                    self.parent.image = image as? UIImage // WARNING SHOWS HERE
                }
            }
        }

I am not sure how to address this. I tried to wrap this in a Task on MainActor.run but got a message that this is invalid in Swift 6.

2      

On the call for the func wrap it in

Task { @MainActor in
  // call to the picker
}

or

DispatchQueue.main.async {
  provider.loadObject(ofClass: UIImage.self ) { image, _ in
      self.parent.image = image as? UIImage
  }
}

2      

I ran into a similar error, it seems to be a bug in Simulator. Choose the second picture then it successfully saves.

https://developer.apple.com/forums/thread/699155

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.