From 19fa1011427c36b5041f1f23bed132d6070cbc29 Mon Sep 17 00:00:00 2001 From: Eugene Mozharovsky Date: Wed, 25 Jan 2017 00:33:47 +0300 Subject: [PATCH] Updated the source files for #18 --- .../ATHImagePickerAssetsViewController.swift | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/Source/ATHImagePickerAssetsViewController.swift b/Source/ATHImagePickerAssetsViewController.swift index 81ec3f1..f5d13cf 100644 --- a/Source/ATHImagePickerAssetsViewController.swift +++ b/Source/ATHImagePickerAssetsViewController.swift @@ -40,14 +40,28 @@ open class ATHImagePickerAssetsViewController: UIViewController, AssetsControlle open var space: CGFloat = 2 public lazy var fetchResult: PHFetchResult = { () -> PHFetchResult in + let collections = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumMyPhotoStream, options: nil) + guard let collection = collections.firstObject else { + return PHAsset.fetchAssets(with: .image, options: nil) + } + let options = PHFetchOptions() - options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] - let fetchResult = PHAsset.fetchAssets(with: .image, options: options) + options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue) + let fetchResult = PHAsset.fetchAssets(in: collection, options: options) return fetchResult }() - public lazy var cachingImageManager = PHCachingImageManager() + public lazy var fetchingOptions: PHImageRequestOptions = { + let options = PHImageRequestOptions() + options.deliveryMode = .highQualityFormat + options.isSynchronous = true + options.isNetworkAccessAllowed = true + + return options + }() + + public lazy var cachingImageManager = PHCachingImageManager.default() as! PHCachingImageManager public var previousPreheatRect: CGRect = .zero @@ -93,7 +107,7 @@ open class ATHImagePickerAssetsViewController: UIViewController, AssetsControlle super.viewWillAppear(animated) if !reloaded { - guard let firstAsset = fetchResult.firstObject else { + guard let firstAsset = fetchResult.lastObject else { if ATHImagePickerAssetsViewController.basicPhoto == nil { let bundle = Bundle(for: self.classForCoder) ATHImagePickerAssetsViewController.basicPhoto = UIImage(named: "basicPhoto", in: bundle, compatibleWith: nil) @@ -206,15 +220,16 @@ extension ATHImagePickerAssetsViewController: UICollectionViewDataSource, UIColl return cell } - let asset = fetchResult[indexPath.item] + let asset = fetchResult[fetchResult.count - indexPath.item - 1] + cachingImageManager.requestImage( for: asset, targetSize: cellSize, contentMode: .aspectFill, - options: nil) { [cell] result, info in - - cell.photoImageView.image = result - + options: fetchingOptions) { result, info in + DispatchQueue.main.async { + cell.photoImageView.image = result + } } cell.backgroundColor = .red @@ -223,16 +238,19 @@ extension ATHImagePickerAssetsViewController: UICollectionViewDataSource, UIColl } public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + let reversedIndex = fetchResult.count - indexPath.item - 1 + guard indexPath.item < fetchResult.count else { return } - let asset = fetchResult[indexPath.item] + let asset = fetchResult[fetchResult.count - indexPath.item - 1] + cachingImageManager.requestImage( for: asset, targetSize: UIScreen.main.bounds.size, contentMode: .aspectFill, - options: nil) { result, info in + options: fetchingOptions) { result, info in if info!["PHImageFileURLKey"] != nil { if let previewController = self.holder.previewController, previewController.state == .folded { let floatingView = self.holder.floatingView @@ -248,7 +266,6 @@ extension ATHImagePickerAssetsViewController: UICollectionViewDataSource, UIColl } } } - } }