|
Hi folks, I'm messing with the image picker to a select a profile photo based on this 2022 WWDC demo: https://developer.apple.com/documentation/photokit/bringing_photos_picker_to_your_swiftui_app It works great, thanks to Apple i've managed to implement the code into my app. But I don't understand how to save the image and display it later in the app. I'm using @AppStorage to store other attributes, and I understand @AppStorage cannot be used to save image data. any pointers? Thank you! |
|
I save my images into the DocumentDirectory, the below is my ImageSaver Class
To implement the above you will an extension to your FileManager , see attached:
Then you call your Image like the below:
Give it a try :) |
|
Thank you, that looks promosing, i'd like to use your code but being a noob i'm having trouble understanding how to implement it. Can you give me any pointers? Here's my code so far
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
You image picker class
where you call the picker
Add the picker
on button click to save
where ever you want to display it
|
|
@Flaneur, Full example with your previous model. Your updated LogoModel, with comments on the additions / modifications (not many):
A sample ContentView to test:
Once you tap the center image the picker will come up for you to pick your image. The save button will save your image in the DocumentDirectory with the name myImage.jpg. You can adapt as per AmitShrivastava's proposal too. Once you save the center image will persist. If you tap again, you start again, but if you don't hit save the image will change but will not persist :) General notes: Your model looks ok, but I would keep my ImageSaver struct and extensions in separate files, but its about readability and be able to use your code in the futre. |
|
Thank you @AmitShrivastava and @Bnerd . What great guys and what a super community :) I've been learning Swift for 2 months and it can be tough going, you guys make it so much easier and effecient. Thank you! |
|
Hi @Bnerd , In fact I used some of your code from example ContentView in a HStack as follows;
and I get the error: Instance member 'writeToDisk' cannot be used on type 'ImageSaver'; did you mean to use a value of this type instead? at:
Any idea why? Google is not helping. |
|
Update: I've spend a couple of hours looking at this, no solution yet but I feel it's something to do with uiImageX, or uiImageToSave. |
|
@flaneur imagines a better way to access images:
Aren't you in luck?! Not sure if you follow other developers, but Stewart Lynch is just finishing up a series where he stores and retrieves images from Core Data. He also touches on retrieving images from the document's directory. This may not answer your questions, but it seems like it's in the same cricket pitch. So you might find some nuggets. See -> My Images Tutorial Book some time, and please return here and tell us what you learned! |
|
I removed something by mistake in my solution... In the ContentView previously you need to iniate an instance of the ImageSaver class like this
Then your Button action becomes like this:
|
|
Super thanks @Bnerd. @Obelix I feel coredata is overkill for what I need to do at present. The doculent directory is ideal. I'm simply storing a profile photo. Sure I need to learn core data, it's on the to-do list. Thanks for the link. |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
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.