This is what I am doing. A user chooses what album they want to use and the number of images they want to use from the album. For example 15 images. When a user navigates to a certain controller view it will display the first image and then they click next it will display the next image… until they reach 15.
I am using the PhotoHelper library for getting the images.
var userAlbumArray: [PhotoAlbum] = []
var options = PhotosHelper.defaultImageFetchOptions
options.deliveryMode = .FastFormat
var fetchOptions = PhotosHelper.FetchOptions()
fetchOptions.count = 15
PhotosHelper.getImagesFromAlbum("Album Name", options: options, fetchOptions: fetchOptions, completion: { result in
switch result {
// When options.synchronous is set to true an array of all assets is fetched.
case .Assets(let images):
()
// when options.synchronous is set to false the system fetches one asset at a time calling this completion handler multiple times
case .Asset(let image):
let alb = PhotoAlbum(title: album.localizedTitle ?? "", thumbnail: image)
userAlbumArray.append(alb)
case .Error:
()
}
})
import Foundation
import UIKit
struct PhotoAlbum {
var title: String
var thumbnail: UIImage?
init(title: String, thumbnail: UIImage?) {
self.title = title
self.thumbnail = thumbnail
}
}
Since the images get loaded asynchronously my idea was to create a PhotoAlbum
object for each image and add it to an array on App initial load. Then save the array to UserDefaults
once the user got the View Controller it will get the images from UserDefaults.
I tried saving it to UserDefault but I was not able to. It might be because of the UIImage.
Am I approaching this correctly or does anyone have a better suggestion?