I am writing an app that has a screensaver which traverses the photos from the Camera Roll of the iPad. I am stuck at getting the image file names to be stored into an array so that i can display the image accordingly.
import Photos
@IBOutlet weak var ssImage: UIImageView!
let fetchOptions = PHFetchOptions()
var arrayPhoto: [UIImage] = []
func FetchPhotos() {
let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
//Get the file names of all the photos and store into arrayPhoto
}
//Gets index of current image from array and increment 1 so as to display the next photo every 5 seconds.
func nextImage() {
let currentIndex = arrayPhoto.index(of: imageView.image ?? UIImage()) ?? -1
var nextIndex = currentIndex+1
nextIndex = arrayPhoto.indices.contains(nextIndex) ? nextIndex : 0
UIView.transition(with: ssImage, duration: 0.5, options: .transitionCrossDissolve, animations: { self.ssImage.image = self.arrayPhoto[nextIndex] }, completion: nil)
ssImage.image = arrayPhoto[nextIndex]
}
func scheduledTimer() {
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(nextImage)), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad
FetchPhotos()
scheduledTimer()
}