|
1) I'm a Beginner so for you my code can be full of bad practice and things done in wrong way. 2) My language is Italian so i try to explain my problem better i can I'm trying to create a simple Image Slideshow in SwiftUI and i found on stackoverflow this code that work very well. This is my model
This where i store all the entry and manage the data
In this view, the forEach loop the storage array and found all images.
There are two problems 1) When i swipe from one image to another, the transition lag. 2) Some images are upside down. Check this video on youtube where i show the problem: https://youtu.be/k8d_RPEVNDo I've tried to pass in the forEach loop an array of 5 images (stored in the assets catalog) and all work fine. Can anyone explain how can i solve the problem? |
|
Hi! I wasn't able to reproduce the amount of lag you showed on my phone, but I believe your issue is caused by this property:
The reason for that is that every time you start to swipe, this property computes the image from the data again. That is a very expensive operation, and running it repeatedly every time you start viewing the image is what slows the app down. So the solution is caching. There are actually two ways you could go about doing it. The first one is simple. Just change your computed
The issue with this approach is that since it's loading the image when the user first swipes to it there might be a stutter when the user first opens the image. The second option is to compute your UIImage when you create the model class. This means the heavy work is done at the start, and then when swiping all is smooth. This is a bit more complex because
You'll also need to change the
This approach also has a big drawback though, and that is that if you plan on having a lot of images, loading them all at the start will cause a lag on app launch, which you don't want. So for large numbers of images I suggest using the first solution. |
|
|
|
SPONSORED Debug 10x faster with Proxyman. Your ultimate tool to capture HTTPs requests/ responses, natively built for iPhone and macOS. You’d be surprised how much you can learn about any system by watching what it does over the network.
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.