|
Hi All! I'm working on Day 77's Challenge (creating an app that saves an image from the user's photo library and then asks them to rename it), but I've been stuck for a while and could really use some help. I can successfully show and use the Image Picker and display the selected image in ContentView, but when it comes to saving things to the Documents directory I'm completely stumped. Every solution I find on Google goes outside of the scope of what's been taught so I'm hoping somebody can point me in the right direction with the concepts taught in the course, thanks so much in advance to anyone willing to help out! The main thing I'm struggling with is getting my UserImage struct (shown below) to conform to Codable so that I can save everything to the documents directory, but UserImage includes a UIImage property, which is causing problems since UIImage doesn't conform to Codable. Paul included the following Code as a hint and I'm sure I'm supposed to use it in some way, but I can't figure out where or how to use it (I've tried making a computed property with it and adding it onto the UIImage struct itself):
Here is my UserImage struct (a type that represents the image that the user selected and its associated info):
Here is ContentView:
And here is the ViewModel for ContentView:
|
|
You haven't declared UserImage as codable, hence the error messages.
Also UserImage is a single image, and you are decoding and encoding an array of images, so think about that carefully.
Finally, Paul has given you the hint to convert UIImage to data - "having an image to disk requires a small extra step: you need to convert your UIImage to Data by calling its jpegData() method". Maybe you have done that elsewhere (calling the |
|
Thanks for the reply! Adding Codable conformance to the struct still produces the error messages, sorry about that I probably should've put that in there to be a bit clearer. I believe that's happening because UIImage doesn't automatically conform to Codable, hence my question. Also, I'm decoding and encoding an array of UserImage because I'm trying to decode and encode the images property, which is an array of UserImage. I thought it was done that way back in the BucketList project to save multiple locations, so I copied that overall structure to this project since I want to encode and decode multiple images to be displayed to the User and not just one. Is there a different way you'd suggest going about it to achieve that goal? Lastly, no I don't have the jpegData() method anywhere in my code because I couldn't figure out where to put it or how to integrate it. I'm assuming it will allow UserImage to conform to Codable by handling the UIImage property, but I can't figure out how to integrate it and actually make it do that, which is the main part of my question and the biggest part of the challenge that I'm struggling with. |
|
Instead of conforming It's not really a good idea to conform types you don't control to protocols you don't control, which is why I would not add |
|
Thanks so much guys! I think I figured it out with roosterboy's suggestion because all my errors are gone and photos seem to be getting saved successfully, but would you mind giving me your thoughts on the way I put it all together? Anything that could be cleaner or that you'd do differently? The biggest thing that I'm not sure about is the nil coalescing with the following line in Initializer 2:
Also, why do I need this line in my encode(to:) method (my code compiles just fine without it):
If I'm using this line for encoding (also in my encode(to:) method:
Here's my updated UserImage struct. Thanks again!
|
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.