//申请权限
PHPhotoLibrary.requestAuthorization({ (status) in
}
// 列出所有系统的智能相册
let smartOptions =PHFetchOptions()
let smartAlbums =PHAssetCollection.fetchAssetCollections(with: .smartAlbum,
subtype: .albumRegular,
options: smartOptions)
//列出所有用户创建的相册
let userCollections =PHCollectionList.fetchTopLevelUserCollections(with:nil)
//获取所有资源
letallPhotosOptions =PHFetchOptions()
//按照创建时间倒序排列
allPhotosOptions.sortDescriptors= [NSSortDescriptor(key:"creationDate",
ascending:false)]
//只获取图片
allPhotosOptions.predicate=NSPredicate(format:"mediaType = %d",
PHAssetMediaType.image.rawValue)
self.assetsFetchResults=PHAsset.fetchAssets(with:PHAssetMediaType.image,
options: allPhotosOptions)
// 初始化和重置缓存
self.imageManager = PHCachingImageManager()
//获取缩略图
self.imageManager.requestImage(for: asset, targetSize:assetGridThumbnailSize,
contentMode:PHImageContentMode.aspectFill,
options:nil) { (image, nfo)in
}
//获取文件名
PHImageManager.default().requestImageData(for:myAsset, options:nil,
resultHandler: {
_,_,_, infoin
})
//获取原图
PHImageManager.default().requestImage(for:myAsset,
targetSize:PHImageManagerMaximumSize, contentMode: .default,
options:nil, resultHandler: {
(image,_: [AnyHashable:Any]?)in
})
//监听资源改变
PHPhotoLibrary.shared().register(self)
//协议PHPhotoLibraryChangeObserver
//当照片库发生变化的时候会触发
func photoLibraryDidChange(_changeInstance:PHChange) { }