獲取所有相冊的名稱和圖片
func getAllAlbumInformation() -> [[PHFetchResult]]{
var allSmartAlbums = [PHFetchResult]()
// 所有圖片
let allOptions = PHFetchOptions()
// 時間排序
allOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let allPhotos = PHAsset.fetchAssetsWithOptions(allOptions)
allSmartAlbums.append(allPhotos)
let smartAlbums:PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.SmartAlbum, subtype: PHAssetCollectionSubtype.AlbumRegular, options: nil)
// 智能相冊---最近添加
let collection = smartAlbums.objectAtIndex(SmartAlbumsType.RecentlyAdded.hashValue) as! PHAssetCollection
let recentely = PHAsset.fetchAssetsInAssetCollection(collection, options: nil)
allSmartAlbums.append(recentely)
// 智能相冊---屏幕截圖
let screenshot = smartAlbums.objectAtIndex(SmartAlbumsType.Screenshots.hashValue) as! PHAssetCollection
let screenshotResult = PHAsset.fetchAssetsInAssetCollection(screenshot, options: nil)
allSmartAlbums.append(screenshotResult)
// 智能相冊---個人收藏
let favorite = smartAlbums.objectAtIndex(SmartAlbumsType.Favorites.hashValue) as! PHAssetCollection
let favoriteResult = PHAsset.fetchAssetsInAssetCollection(favorite, options: nil)
allSmartAlbums.append(favoriteResult)
// 個人自定義相冊
let userDeterminAlbums = PHCollectionList.fetchTopLevelUserCollectionsWithOptions(nil)
let allAlbums = [allSmartAlbums,[userDeterminAlbums]]
PHPhotoLibrary.sharedPhotoLibrary().registerChangeObserver(self)
return allAlbums
}
通過上面的方法獲取了所有的相冊分類和圖片,下面解析這個圖片
let all = getAllAlbumInformation()
let singleAsset = all[0][0][8] as! PHAsset
let options = PHImageRequestOptions()
// deliveryMode 控制獲取的圖像的質(zhì)量
options.deliveryMode = .HighQualityFormat
//networkAccessAllowed是否允許網(wǎng)絡(luò)請求,可以從icloud獲取更加清晰的圖像
options.networkAccessAllowed = true
options.progressHandler = { (progress, error, stop, info) in
//可以控制進度條
print(progress)
}
//resizeMode 設(shè)置為.Exact 則返回圖像必須和目標(biāo)大小相匹配,并且圖像質(zhì)量也為高質(zhì)量圖像,而設(shè)置為 .Fast 則請求的效率更高,但返回的圖像可能和目標(biāo)大小不一樣并且質(zhì)量較低。
//options 的 synchronous 屬性惠拭,默認為 NO,同步操作。注意:當(dāng) synchronous 設(shè)為 true 時,deliveryMode 屬性就會被忽略沃疮,并被當(dāng)作 .HighQualityFormat 來處理。
//normalizedCropRect 設(shè)置裁剪大小 如CGRect(origin: CGPoint(x: 0,y: 0), size: CGSize(width: 200,height: 200))
PHImageManager.defaultManager().requestImageForAsset(singleAsset, targetSize: PHImageManagerMaximumSize,contentMode: PHImageContentMode.AspectFill, options: options, resultHandler: { (result, info) in
//result 是 拿到的UIImage
})
上面代碼有什么不正確的地方,請在下方留言告訴我.如果我的文章對您有所幫助,我會非常高興!
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者