From 012f3c6ca8ee588fc2ac60cbfc30c4ca4d22733d Mon Sep 17 00:00:00 2001 From: tilltue Date: Sat, 23 Dec 2017 11:37:34 +0900 Subject: [PATCH] support get all album --- Example/TLPhotoPicker/ViewController.swift | 9 ++++- TLPhotoPicker.podspec | 2 +- TLPhotoPicker/Classes/TLPhotoLibrary.swift | 33 ++++++++++++++++--- .../TLPhotosPickerViewController.swift | 3 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Example/TLPhotoPicker/ViewController.swift b/Example/TLPhotoPicker/ViewController.swift index e1d1d17d..095fde26 100644 --- a/Example/TLPhotoPicker/ViewController.swift +++ b/Example/TLPhotoPicker/ViewController.swift @@ -24,6 +24,7 @@ class ViewController: UIViewController,TLPhotosPickerViewControllerDelegate { } var configure = TLPhotosPickerConfigure() configure.numberOfColumn = 3 + configure.getAllAlbum = true //configure.maxSelectedAssets = 10 //configure.nibSet = (nibName: "CustomCell_Instagram", bundle: Bundle.main) viewController.configure = configure @@ -74,7 +75,13 @@ class ViewController: UIViewController,TLPhotosPickerViewControllerDelegate { asset.tempCopyMediaFile(progressBlock: { (progress) in print(progress) }, completionBlock: { (url, mimeType) in - print(url) + func fileSize(_ url: URL?) -> Int? { + do { + guard let fileSize = try url?.resourceValues(forKeys: [.fileSizeKey]).fileSize else { return nil } + return fileSize + }catch { return nil } + } + print(fileSize(url)) print(mimeType) }) } diff --git a/TLPhotoPicker.podspec b/TLPhotoPicker.podspec index 8af8b32f..81eb9750 100644 --- a/TLPhotoPicker.podspec +++ b/TLPhotoPicker.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'TLPhotoPicker' - s.version = '1.4.1' + s.version = '1.4.2' s.summary = 'multiple phassets picker for iOS lib. like facebook' # This description is used to generate tags and improve search results. diff --git a/TLPhotoPicker/Classes/TLPhotoLibrary.swift b/TLPhotoPicker/Classes/TLPhotoLibrary.swift index f244b0d8..687b1956 100644 --- a/TLPhotoPicker/Classes/TLPhotoLibrary.swift +++ b/TLPhotoPicker/Classes/TLPhotoLibrary.swift @@ -24,7 +24,7 @@ class TLPhotoLibrary { }() deinit { - //print("deinit TLPhotoLibrary") +// print("deinit TLPhotoLibrary") } @discardableResult @@ -132,9 +132,31 @@ extension TLPhotoLibrary { return PHAsset.fetchAssets(in: phAssetCollection, options: options) } - func fetchCollection(allowedVideo: Bool = true, useCameraButton: Bool = true, mediaType: PHAssetMediaType? = nil, maxVideoDuration:TimeInterval? = nil,options: PHFetchOptions? = nil) { - - let options = options ?? getOption() + func fetchCollection(configure: TLPhotosPickerConfigure) { + let allowedVideo = configure.allowedVideo + let useCameraButton = configure.usedCameraButton + let mediaType = configure.mediaType + let maxVideoDuration = configure.maxVideoDuration + let getAllAlbum = configure.getAllAlbum + let options = configure.fetchOption ?? getOption() + + @discardableResult + func getAlbum(subType: PHAssetCollectionSubtype, result: inout [TLAssetsCollection]) { + let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: subType, options: nil) + var collections = [PHAssetCollection]() + fetchCollection.enumerateObjects { (collection, index, _) in + collections.append(collection) + } + for collection in collections { + if !result.contains(where: { $0.localIdentifier == collection.localIdentifier }) { + var assetsCollection = TLAssetsCollection(collection: collection) + assetsCollection.fetchResult = PHAsset.fetchAssets(in: collection, options: options) + if assetsCollection.count > 0 { + result.append(assetsCollection) + } + } + } + } @discardableResult func getSmartAlbum(subType: PHAssetCollectionSubtype, result: inout [TLAssetsCollection]) -> TLAssetsCollection? { @@ -176,6 +198,9 @@ extension TLPhotoLibrary { getSmartAlbum(subType: .smartAlbumPanoramas, result: &assetCollections) //Favorites getSmartAlbum(subType: .smartAlbumFavorites, result: &assetCollections) + if getAllAlbum { + getAlbum(subType: .any, result: &assetCollections) + } if allowedVideo { //Videos getSmartAlbum(subType: .smartAlbumVideos, result: &assetCollections) diff --git a/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift b/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift index 815560aa..26455317 100644 --- a/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift +++ b/TLPhotoPicker/Classes/TLPhotosPickerViewController.swift @@ -36,6 +36,7 @@ public struct TLPhotosPickerConfigure { public var allowedLivePhotos = true public var allowedVideo = true public var allowedVideoRecording = true + public var getAllAlbum = false public var maxVideoDuration:TimeInterval? = nil public var autoPlay = true public var muteAudio = false @@ -278,7 +279,7 @@ extension TLPhotosPickerViewController { fileprivate func initPhotoLibrary() { if PHPhotoLibrary.authorizationStatus() == .authorized { self.photoLibrary.delegate = self - self.photoLibrary.fetchCollection(allowedVideo: self.allowedVideo, useCameraButton: self.usedCameraButton, mediaType: self.configure.mediaType, maxVideoDuration:self.configure.maxVideoDuration, options: self.configure.fetchOption) + self.photoLibrary.fetchCollection(configure: self.configure) }else{ //self.dismiss(animated: true, completion: nil) }