I'll try to explain this as simple as possible. I've made an app that modifies an UIImage based on user actions.
To be able to share/save the resulting image, I added a ShareLink to my view.
Every time the user makes changes to the image the app consumes memory which is never released.
When I remove the ShareLink() the memory is released and works as expected.
Have I missed something regarding how ShareLink() works? I have read the Apple documentation, but I don't get any wiser.
Btw, I am using a @State-variable for my controller, which should be ok since it's not an ObservableObject.
This is the basic structure of my app, very simplified.
class ImageController() {
func modifyImage(photo: UIImage?) -> async UIImage? {
// do stuff here
return resultingUIImage
}
}
struct ContentView: View {
@State private var photo: UIImage?
@State private var imageController: ImageController()
var body: some View {
VStack {
Image(uiImage: photo)
HStack {
Button("Modify the image") {
Task {
let newPhoto = await modifyImage(photo)
DispatchQueue.main.async {
self.photo = newPhoto
}
}
}
// when removing ShareLink() below the app doesn't leak memory
ShareLink(item: Image(uiImage: photo), preview: SharePreview("Description", image: Image(uiImage: photo))
}
}
}
}
I have now solved it, but not sure if it's the correct way. This is what I did: