Skip to content

Commit

Permalink
fixed #61
Browse files Browse the repository at this point in the history
  • Loading branch information
tilltue committed Jan 2, 2018
1 parent 64b97f3 commit 6c9aee3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions TLPhotoPicker/Classes/TLAssetsCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ struct TLAssetsCollection {
var localIdentifier: String
var count: Int {
get {
guard let count = self.fetchResult?.count, count > 0 else { return 0 }
guard let count = self.fetchResult?.count, count > 0 else { return self.useCameraButton ? 1 : 0 }
return count + (self.useCameraButton ? 1 : 0)
}
}
Expand All @@ -229,14 +229,15 @@ struct TLAssetsCollection {
func getAsset(at index: Int) -> PHAsset? {
if self.useCameraButton && index == 0 { return nil }
let index = index - (self.useCameraButton ? 1 : 0)
return self.fetchResult?.object(at: max(index,0))
guard let result = self.fetchResult, index < result.count else { return nil }
return result.object(at: max(index,0))
}

func getTLAsset(at index: Int) -> TLPHAsset? {
if self.useCameraButton && index == 0 { return nil }
let index = index - (self.useCameraButton ? 1 : 0)
guard let asset = self.fetchResult?.object(at: max(index,0)) else { return nil }
return TLPHAsset(asset: asset)
guard let result = self.fetchResult, index < result.count else { return nil }
return TLPHAsset(asset: result.object(at: max(index,0)))
}

func getAssets(at range: CountableClosedRange<Int>) -> [PHAsset]? {
Expand Down
6 changes: 3 additions & 3 deletions TLPhotoPicker/Classes/TLPhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ extension TLPhotoLibrary {
}

@discardableResult
func getSmartAlbum(subType: PHAssetCollectionSubtype, result: inout [TLAssetsCollection]) -> TLAssetsCollection? {
func getSmartAlbum(subType: PHAssetCollectionSubtype, useCameraButton: Bool = false, result: inout [TLAssetsCollection]) -> TLAssetsCollection? {
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: subType, options: nil)
if let collection = fetchCollection.firstObject, !result.contains(where: { $0.localIdentifier == collection.localIdentifier }) {
var assetsCollection = TLAssetsCollection(collection: collection)
assetsCollection.fetchResult = PHAsset.fetchAssets(in: collection, options: options)
if assetsCollection.count > 0 {
if assetsCollection.count > 0 || useCameraButton {
result.append(assetsCollection)
return assetsCollection
}
Expand All @@ -189,7 +189,7 @@ extension TLPhotoLibrary {
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
var assetCollections = [TLAssetsCollection]()
//Camera Roll
let camerarollCollection = getSmartAlbum(subType: .smartAlbumUserLibrary, result: &assetCollections)
let camerarollCollection = getSmartAlbum(subType: .smartAlbumUserLibrary, useCameraButton: useCameraButton, result: &assetCollections)
if var cameraRoll = camerarollCollection {
cameraRoll.useCameraButton = useCameraButton
assetCollections[0] = cameraRoll
Expand Down
2 changes: 1 addition & 1 deletion TLPhotoPicker/Classes/TLPhotosPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ extension TLPhotosPickerViewController: UITableViewDelegate,UITableViewDataSourc
let collection = self.collections[indexPath.row]
cell.thumbImageView.image = collection.thumbnail
cell.titleLabel.text = collection.title
cell.subTitleLabel.text = "\(collection.count)"
cell.subTitleLabel.text = "\(collection.fetchResult?.count ?? 0)"
if let phAsset = collection.getAsset(at: collection.useCameraButton ? 1 : 0), collection.thumbnail == nil {
let scale = UIScreen.main.scale
let size = CGSize(width: 80*scale, height: 80*scale)
Expand Down

0 comments on commit 6c9aee3

Please sign in to comment.