Search code examples
iosswiftuipageviewcontroller

How can I implement the photos swiping like iOS Photos app using the UIPageViewController?


Suppose I have a large array of photoIds which I can use in the Photos APP requestPhoto API to fetch a photo. How can I implement the UIPageViewController to accommodate a large array as such. Wouldn't creating a view controller for each of these photos on swiping left/right cause a degradation of performance of the App. Could anyone suggest a better approach to this? I've looked everywhere but couldn't find anything. I suppose another way to frame this question would be 'How can I implement the UIPageViewController with a dynamic set of data instead of a static number?


Solution

  • For anyone else struggling with the same issue. I found this video very helpful. https://www.youtube.com/watch?v=ItdPXinQOp8?

    According to this, I made an array with lazy var that pushes all the view controllers in the array. The view controller is only loaded when it is needed so that prevents any performance degradation. So it doesn't matter if you have n number of view controllers.

    So basically the approach will be:

    1. Have an array with all the imageIDs you want to swipe through
    2. Make a lazy var newArray which will iterate through the imageIDs(Array) and fetch the images with an api call. Dont worry it will only call the api when required. Initialize the Detailed View controller with the received image and push viewcontroller in newArray.
    3. Set up UIPagecontroller delegates as usual using the newly formed array of all the viewcontrollers.