获取所有相册的名称和图片
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 控制获取的图像的质量
options.deliveryMode = .HighQualityFormat
//networkAccessAllowed是否允许网络请求,可以从icloud获取更加清晰的图像
options.networkAccessAllowed = true
options.progressHandler = { (progress, error, stop, info) in
//可以控制进度条
print(progress)
}
//resizeMode 设置为.Exact 则返回图像必须和目标大小相匹配,并且图像质量也为高质量图像,而设置为 .Fast 则请求的效率更高,但返回的图像可能和目标大小不一样并且质量较低。
//options 的 synchronous 属性,默认为 NO,同步操作。注意:当 synchronous 设为 true 时,deliveryMode 属性就会被忽略,并被当作 .HighQualityFormat 来处理。
//normalizedCropRect 设置裁剪大小 如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
})
上面代码有什么不正确的地方,请在下方留言告诉我.如果我的文章对您有所帮助,我会非常高兴!
网友评论