【纯代码】Swift相册照片选择-支持单选或多选

本文介绍如何在Swift中实现相册照片的选择,支持单选和多选。通过代码示例,展示相册照片选择的功能实现,并提供实际运行的效果图。
摘要由CSDN通过智能技术生成
//
//  NAPublishAlbumTableViewController.swift
////
//  Created by  on 2019/3/23.
//  Copyright © 2019年 . All rights reserved.
//

import UIKit
import Photos

typealias HandlePhotos = ([PHAsset], [UIImage]) -> Void
class HandleSelectionPhotosManager: NSObject {
    static let share = HandleSelectionPhotosManager()
    
    var maxCount: Int = 0
    var callbackPhotos: HandlePhotos?
    
    private override init() {
        super.init()
    }
    
    func getSelectedPhotos(with count: Int, callback completeHandle: HandlePhotos? ) {
        // 限制长度
        maxCount = count < 1 ? 1 : (count > 9 ? 9 : count)
        self.callbackPhotos = completeHandle
    }
}

/// 后期可用来对相应的英文文件夹修改为汉语名,暂时未使用
enum AlbumTransformChina: String {
    case Favorites
    case RecentlyDeleted = "Recently Deleted"
    case Screenshots
    
    func chinaName() -> String {
        switch  self {
        case .Favorites:
            return "最爱"
        case .RecentlyDeleted:
            return "最近删除"
        case .Screenshots:
            return "手机截屏"
        }
    }
}


/// - albumAllPhotos: 所有
/// - albumSmartAlbums: 智能
/// - albumUserCollection: 收藏
enum AlbumSession: Int {
    case albumAllPhotos = 0
    case albumSmartAlbums
    case albumUserCollection
    
    static let count = 2
}
class NAPublishAlbumTableViewController : UITableViewController{
    // MARK: - ?Properties
    fileprivate var allPhotos: PHFetchResult<PHAsset>!
    fileprivate var smartAlbums: PHFetchResult<PHAssetCollection>!
    fileprivate var userCollections: PHFetchResult<PHCollection>!
    private let sectionTitles = ["", "智能相册", "相册"]
    
    fileprivate var MaxCount: Int = 0
    fileprivate var handleSelectionAction: (([String], [String]) -> Void)?
    
    // MARK: - ?Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        addCancleItem()
        
        fetchAlbumsFromSystemAlbum()
    }
    
    deinit {
        print(" ------1 deinit")
        PHPhotoLibrary.shared().unregisterChangeObserver(self)
    }
    
    // MARK: - ?Private
    /// 获取所有系统相册概览信息
    private func fetchAlbumsFromSystemAlbum() {
        let allPhotoOptions = PHFetchOptions()
        // 时间排序
        allPhotoOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
        allPhotos = PHAsset.fetchAssets(with: allPhotoOptions)
        
        smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: nil)
        
        userCollections = PHCollectionList.fetchTopLevelUserCollections(with: nil)
        
        // 监测系统相册增加,即使用期间是否拍照
        PHPhotoLibrary.shared().register(self)
        
        // 注册cell
        tableView.register(MasterTableViewCell.self, forCellReuseIdentifier: MasterTableViewCell.cellIdentifier)
    }
    
    
    /// 添加取消按钮
    private func addCancleItem() {
        let barItem = UIBarButtonItem(title: "取消", style: .plain, target: self, action: #selector(dismissAction))
        navigationItem.rightBarButtonItem = barItem
    }
    @objc func dismissAction() {
        dismiss(animated: true, completion: nil)
    }
    
    // MARK: - ?UITableViewDelegate & UITableViewDataSource
    override func numberOfSections(in tableView: UITableView) -> Int {
        return AlbumSession.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch AlbumSession(rawValue: section)! {
        case .albumAllPhotos: return 1
        case .albumSmartAlbums: return smartAlbums.count
        case .albumUserCollection: return userCollections.count
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MasterTableViewCell.cellIdentifier, for: indexPath) as! MasterTableViewCell
        cell.selectionStyle = .none
        
        switch AlbumSession(rawValue: indexPath.section)! {
        case .albumAllPhotos:
            cell.asset = allPhotos.firstObject
            cell.albumTitleAndCount = ("所有照片", allPhotos.count)
        case .albumSmartAlbums:
            let collection = smartAlbums.object(at: indexPath.row)
            cell.asset = PHAsset.fetchAssets(in: collection, options: nil).firstObject
            cell.albumTitleAndCount = (collection.localizedTitle, PHAsset.fetchAssets(in: collection, options: nil).count)
        case .albumUserCollection:
            let collection = userCollections.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值