I know that there is already this question on SO, but I don't think that the given answer is satisfying/complete: How can IOS Photos app can show hundreds of photos in one screen?
What I want to achieve
I want something like the image selection in whatsapp (iOS) (see screenshot). When you open the camera, there is also a horizontal slider where you can see all images from your gallery.
What I tried
Right now I have the following code in my appDelegate:
let options = PHFetchOptions()
options.sortDescriptors = [
NSSortDescriptor(key: "creationDate", ascending: true)
]
if let results = PHAsset.fetchAssetsWithMediaType(.Image, options: options) {
results.enumerateObjectsUsingBlock { (object, idx, _) in
if let asset = object as? PHAsset {
Variables.assets.append(asset)
}
}
println(Variables.assets.count)
Variables.imageManager.startCachingImagesForAssets(Variables.assets, targetSize: CGSizeMake(viewWidth, viewWidth), contentMode: .AspectFill, options: self.requestOptions)
}
Later I load the images in a UITableViewController and call the following function on scroll:
func fetchPhotoAtIndex(index : Int) {
let asset = Variables.assets[Variables.assets.count - 1 - index] as PHAsset
Variables.imageManager.requestImageForAsset(asset, targetSize: CGSizeMake(self.viewWidth, self.viewWidth), contentMode: .AspectFill, options: self.requestOptions, resultHandler: { (image, _) in
println("\(asset.localIdentifier) \(asset.creationDate) ")
[...]
self.tableView.reloadData()
})
}
This works well but I have the problem that on every app start all my assets get cached. Is this the right approach for my problem? How can I display the gallery photos in realtime like whatsapp does?