AssetsPickerViewController 2.9.7

AssetsPickerViewController 2.9.7

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最后发布2020年9月
SPM支持 SPM

HFCoreUI 维护。




现在 iOS 14 默认支持多个资产选择器。由于许多原因,我建议使用 PHPickerViewController 而不是这个选择器。

AssetsPickerViewController

Version License Platform

可自定义的资产选择器控制器,支持选择多张照片和视频,完全使用 Swift 编写。

评论

AssetsPickerViewController 类似于 iOS 中的 Photos 应用。

如果您发现了任何错误——甚至是开发分支中的错误,请不要犹豫,为它提出一个问题。

欢迎任何关于新特性的建议、建议和 pull request。

只需在 Web 模拟器中尝试,不要浪费时间。

https://appetize.io/app/752b6azuj3d3varvmu1hkwuuqm

截图

相册与资产友好的UI

albums_portrait photos_portrait photos_portrait

支持iPad

ipad_landscape

在方向改变时保持焦点索引。

photos_landscape

处理空或无权限的情况。

no_photos no_permission

可自定义相册与资产布局

customize_album customize_asset

3D Touch用于预览

3d_touch

已完成功能

  • 相册与照片控制器友好的iOS UI

  • 选择相册

  • 选择多个照片和视频

  • 相册和照片中库变更的实时同步

  • 显示/隐藏空相册的选项

  • 显示/隐藏“隐藏”相册的选项

  • 自定义相册单元格

  • 通过PHFetchOptions或过滤块自定义相册排序

  • 通过PHFetchOptions或过滤块自定义相册过滤

  • 自定义资产单元格

  • 通过PHFetchOptions自定义资产排序

  • 通过PHFetchOptions自定义资产过滤

  • 支持iPad

  • 3D(触摸)预览 - (静态图片、Live照片和视频)

  • 支持多种语言(德语、法语、西班牙语、中文、日语、阿拉伯语、西班牙语、韩语、印尼语、俄语、土耳其语、意大利语等)

  • 在选择选择器控制器之前设置选定的资产

  • 支持从iOS 13开始的支持暗黑模式

  • 在选择器内部自动拍摄并选择照片或视频

  • 通过拖拽单元格进行多选(从iOS 13开始)

  • 支持SPM(Swift Package Manager)

待办功能

  • 在选择之前裁剪图片

基本用法

要运行示例项目,首先克隆存储库,然后从示例目录中运行pod install

// to show
let picker = AssetsPickerViewController()
picker.pickerDelegate = self
present(picker, animated: true, completion: nil)

// to handle
extension SimpleExampleController: AssetsPickerViewControllerDelegate {
    
    func assetsPickerCannotAccessPhotoLibrary(controller: AssetsPickerViewController) {}
    func assetsPickerDidCancel(controller: AssetsPickerViewController) {}
    func assetsPicker(controller: AssetsPickerViewController, selected assets: [PHAsset]) {
        // do your job with selected assets
    }
    func assetsPicker(controller: AssetsPickerViewController, shouldSelect asset: PHAsset, at indexPath: IndexPath) -> Bool {
        return true
    }
    func assetsPicker(controller: AssetsPickerViewController, didSelect asset: PHAsset, at indexPath: IndexPath) {}
    func assetsPicker(controller: AssetsPickerViewController, shouldDeselect asset: PHAsset, at indexPath: IndexPath) -> Bool {
        return true
    }
    func assetsPicker(controller: AssetsPickerViewController, didDeselect asset: PHAsset, at indexPath: IndexPath) {}
}

额外功能

基本

要隐藏空相册,

pickerConfig.albumIsShowEmptyAlbum = false

要显示“隐藏”相册,

pickerConfig.albumIsShowHiddenAlbum = true

在选择组件呈现之前设置预选资源,

pickerConfig.selectedAssets = self.assets

要限制选择资源的数量,

func assetsPicker(controller: AssetsPickerViewController, shouldSelect asset: PHAsset, at indexPath: IndexPath) -> Bool {   
    if controller.selectedAssets.count > 3 {
        // do your job here
        return false
    }
    return true
}

要启用单图选择模式,到达限制时取消选择所有项目,

func assetsPicker(controller: AssetsPickerViewController, shouldSelect asset: PHAsset, at indexPath: IndexPath) -> Bool {   
    if controller.selectedAssets.count > 0 {
        controller.photoViewController.deselectAll()
    }
    return true
}

自动取消选择最旧的选择资源以达到限制的数量,

pickerConfig.assetsMaximumSelectionCount = 5

外观

应用自定义相册单元格,

pickerConfig.albumCellType = CustomAlbumCell.classForCoder()
// and implement your own UICollectionViewCell which conforms to AssetsAlbumCellProtocol

应用自定义资源单元格,

pickerConfig.assetCellType = CustomAssetCell.classForCoder()
// and implement your own UICollectionViewCell which conforms to AssetsPhotoCellProtocol

排序

按 PHFetchOptions 排序相册,

let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor(key: "estimatedAssetCount", ascending: true)]
        
pickerConfig.albumFetchOptions = [
    .smartAlbum: options
]

按特定原因排序包块,

pickerConfig.albumComparator = { (albumType, leftEntry, rightEntry) -> Bool in
    // return: Is leftEntry ordered before the rightEntry?
    switch albumType {
    case .smartAlbum:
        return leftEntry.album.assetCollectionSubtype.rawValue < rightEntry.album.assetCollectionSubtype.rawValue
    case .album:
        return leftEntry.result.count < rightEntry.result.count     // ascending order by asset count
    case .moment:
        return true
    }
}

按 PHFetchOptions 排序资源,

let options = PHFetchOptions()
options.sortDescriptors = [
    NSSortDescriptor(key: "pixelWidth", ascending: true),
    NSSortDescriptor(key: "pixelHeight", ascending: true)
]

pickerConfig.assetFetchOptions = [
    .smartAlbum: options
]

筛选

按 PHFetchOptions 筛选相册,

let options = PHFetchOptions()
options.predicate = NSPredicate(format: "estimatedAssetCount = 0")
pickerConfig.albumFetchOptions = [.smartAlbum: options]

按特定原因筛选包块,

// return true to include, false to discard.
let smartAlbumFilter: ((PHAssetCollection, PHFetchResult<PHAsset>) -> Bool) = { (album, fetchResult) in
    // filter by album object
    if album.assetCollectionSubtype == .smartAlbumBursts { return false }
    if album.assetCollectionSubtype == .smartAlbumTimelapses { return false }
    if album.assetCollectionSubtype == .smartAlbumFavorites { return false }
            
    // filter by fetch result
    if fetchResult.count > 50 {
        return true     // only shows albums that contains more than 50 assets
    } else {
        return false    //
    }
}
pickerConfig.albumFilter = [
    .smartAlbum: smartAlbumFilter
]

按 PHFetchOptions 筛选资源,

let options = PHFetchOptions()
options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
options.sortDescriptors = [NSSortDescriptor(key: "duration", ascending: true)]
        
pickerConfig.assetFetchOptions = [
    .smartAlbum: options,
    .album: options
]

自定义本地化支持

要设置自己的自定义字符串,只需使用 AssetsPickerConfig 类型的静态 customStringConfig 属性,该属性为 AssetsPickerCustomStringConfig

重写每个字符串

AssetsPickerConfig.customStringConfig = [
    .cancel: "Cancel",
    .done: "Done",
    .titleAlbums: "Albums",
    .titleSectionMyAlbums: "My Albums",
    .footerPhotos: "%@ Photos",
    .footerVideos: "%@ Videos",
    .footerItems: "%@ Photos, %@ Videos",
    .titleSelectedPhoto: "%@ Photo Selected",
    .titleSelectedPhotos: "%@ Photos Selected",
    .titleSelectedVideo: "%@ Video Selected",
    .titleSelectedVideos: "%@ Videos Selected",
    .titleSelectedItems: "%@ Items Selected",
    .titleNoItems: "No Photos or Videos",
    .messageNoItems: "You can take photos and videos using the camera, or sync photos and videos onto your %@ using iTunes.",
    .messageNoItemsCamera: "You can sync photos and videos onto your %@ using iTunes.",
    .titleNoPermission: "This app does not have access to your photos or videos.",
    .messageNoPermission: "You can enable access in Privacy Settings.",
]

重写特定的字符串

AssetsPickerConfig.customStringConfig = [
    .titleNoItems: "No Photos or Videos",
    .messageNoItems: "You can take photos and videos using the camera, or sync photos and videos onto your %@ using iTunes.",
    .messageNoItemsCamera: "You can sync photos and videos onto your %@ using iTunes.",
    .titleNoPermission: "This app does not have access to your photos or videos.",
    .messageNoPermission: "You can enable access in Privacy Settings.",
]

注意:如果未正确设置字符串,可能导致问题。

需求与依赖

Xcode10.2, Swift 5, iOS 10.0

使用 SnapKit 创建库内的UI。感谢SnapKit开发团队为我们创造了如此漂亮的工作。

如果您的应用的部署目标版本大于或等于11.0,则可以使用最新版本的SnapKit;否则,您必须将SnapKit的版本固定到5.0.0

安装

AssetsPickerViewController可通过CocoaPods获取。要安装,只需将以下行添加到您的Podfile中

pod 'AssetsPickerViewController', '~> 2.0'

不再支持Swift 4。

作者

DragonCherry, [email protected]

许可证

AssetsPickerViewController遵守MIT许可。有关更多信息,请参阅LICENSE文件。

MIT许可证

版权所有 © 2017 DragonCherry

以下条件之下,授予任何人获得本软件及其相关文档副本(“软件”)的副本的权利,免收费用,允许任何人不受限制地使用软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可或转让软件的副本,并允许向获得软件的人提供这些权利

上述版权声明和本许可声明应包含在软件的所有副本或实质部分的副本中。

软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定用途和侵权保证。在任何情况下,作者或著作权人都不应对任何索赔、损害或其他责任负责,无论这种责任是基于合同、侵权或其他原因,由软件或软件的使用或其他方式引起