Skip to content

Commit

Permalink
Updated the source files for #18
Browse files Browse the repository at this point in the history
  • Loading branch information
mozharovsky committed Jan 24, 2017
1 parent c2d3f1b commit 19fa101
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions Source/ATHImagePickerAssetsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,28 @@ open class ATHImagePickerAssetsViewController: UIViewController, AssetsControlle
open var space: CGFloat = 2

public lazy var fetchResult: PHFetchResult = { () -> PHFetchResult<PHAsset> 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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -248,7 +266,6 @@ extension ATHImagePickerAssetsViewController: UICollectionViewDataSource, UIColl
}
}
}

}
}

Expand Down

0 comments on commit 19fa101

Please sign in to comment.