貢獻做者 -【XJDomain】
博客XJ: https://my.oschina.net/shengbingli/blog
GitHub直播地址: https://github.com/lishengbing/XJDomainLivegit
01-PHPhotoLibrary :是一個資源庫,可以獲取相冊權限和對相冊的操做github
02-PHAssetCollection : 是一個資源集合對象,至關於一個相冊的概念
01- 拿到全部系統的智能相冊組:swift
// 1.1 列出全部系統的智能相冊 let smartAlbumsFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil)
02-取出全部用戶自定義的相冊組數組
// 1.3取出全部用戶建立的相冊 let topLevelUserCollections = PHCollectionList.fetchTopLevelUserCollections(with: nil)
03-PHFetchResult : 是一個結果集,一個範集類,上面獲取到的智能相冊組和自定義相冊組 就是被封裝成改類中返回給咱們的,咱們須要遍歷上面 相冊資源組 拿到咱們每個相冊裏面的全部單獨資源app
01-分別遍歷上面的智能相冊和用戶自定義相冊,獲得每個相冊中的單獨資源信息框架
// 懶加載模型數組:相簿列表數據 fileprivate lazy var albums : [AlbumItem] = [AlbumItem]() // 獲取相冊列表 fileprivate func getAlbumLists(_ collection : PHFetchResult<PHAssetCollection>) { for index in 0..<collection.count { // 獲取當前相簿內的照片 let resultOptions = PHFetchOptions() resultOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] // 只獲取圖片資源(排除音視頻資源) resultOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue) let assetCollection = collection[index] let assetFetchResult = PHAsset.fetchAssets(in: assetCollection, options: resultOptions) // 沒有圖片的空相簿不顯示 if assetFetchResult.count > 0 { let items = AlbumItem(title: assetCollection.localizedTitle!, fetchResult: assetFetchResult as! PHFetchResult<AnyObject>) let resource = getFirstImageAtPHFetchResult(assetFetchReult: assetFetchResult) items.firstImage = resource.firstImage items.info = resource.info albums.append(items) } } } // 遍歷assetFetchReult結果集,能夠獲得每個單獨照片 let cacheImageManager = PHCachingImageManager() let asset = assetFetchReult.firstObject! cacheImageManager.stopCachingImagesForAllAssets() cacheImageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: nil) { (image : UIImage?, info : [AnyHashable : Any]?) in }
04-PHAsset 是一個獨立的資源對象,能夠經過類方法對PHCollection對象進行遍歷,得到存放Asset對象的結果集,能夠直接得到資源的規格數據,若想得到圖片以及原圖等資源,須要配合PHImageManager對象,繼承自PHObject.fetch