Commit 5caefc91 authored by lujunye's avatar lujunye

32

parent 66f4791b
......@@ -30,13 +30,13 @@ class HGImagePickerController: UIViewController {
// 列出所有系统的智能相册
let smartOptions = PHFetchOptions()
let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum,
subtype: .albumRegular,
options: smartOptions)
subtype: .albumRegular,
options: smartOptions)
self.convertCollection(collection: smartAlbums)
//列出所有用户创建的相册
let userCollections = PHCollectionList.fetchTopLevelUserCollections(with: nil)
self.convertCollection(collection: userCollections
as! PHFetchResult<PHAssetCollection>)
as! PHFetchResult<PHAssetCollection>)
//相册按包含的照片数量排序(降序)
self.items.sort { (item1, item2) -> Bool in
......@@ -81,22 +81,25 @@ class HGImagePickerController: UIViewController {
private func convertCollection(collection:PHFetchResult<PHAssetCollection>){
for i in 0..<collection.count{
//获取出但前相簿内的图片
let resultsOptions = PHFetchOptions()
resultsOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",
ascending: false)]
resultsOptions.predicate = NSPredicate(format: "mediaType = %d",
PHAssetMediaType.image.rawValue)
let c = collection[i]
let assetsFetchResult = PHAsset.fetchAssets(in: c , options: resultsOptions)
//没有图片的空相簿不显示
if assetsFetchResult.count > 0 {
let title = titleOfAlbumForChinse(title: c.localizedTitle)
items.append(HGImageAlbumItem(title: title,
fetchResult: assetsFetchResult))
if c.isKind(of: PHAssetCollection.self){
let assetsFetchResult = PHAsset.fetchAssets(in: c , options: resultsOptions)
//没有图片的空相簿不显示
if assetsFetchResult.count > 0 {
let title = titleOfAlbumForChinse(title: c.localizedTitle)
items.append(HGImageAlbumItem(title: title,
fetchResult: assetsFetchResult))
}
}
}
}
//由于系统返回的相册集名称为英文,我们需要转换为中文
private func titleOfAlbumForChinse(title:String?) -> String? {
if title == "Slo-mo" {
......@@ -133,8 +136,8 @@ class HGImagePickerController: UIViewController {
if segue.identifier == "showImages"{
//获取照片展示控制器
guard let imageCollectionVC = segue.destination
as? HGImageCollectionViewController,
let cell = sender as? HGImagePickerCell else{
as? HGImageCollectionViewController,
let cell = sender as? HGImagePickerCell else{
return
}
//设置回调函数
......@@ -161,7 +164,7 @@ class HGImagePickerController: UIViewController {
extension HGImagePickerController:UITableViewDelegate,UITableViewDataSource{
//设置单元格内容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
-> UITableViewCell {
//同一形式的单元格重复使用,在声明时已注册
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as! HGImagePickerCell
......@@ -186,7 +189,7 @@ extension UIViewController {
//HGImagePicker提供给外部调用的接口,同于显示图片选择页面
func presentHGImagePicker(maxSelected:Int = Int.max,
completeHandler:((_ assets:[PHAsset])->())?)
-> HGImagePickerController?{
-> HGImagePickerController?{
//获取图片选择视图控制器
if let vc = UIStoryboard(name: "HGImage", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "imagePickerVC")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment