Skip to content

Commit

Permalink
1.代码结构优化
Browse files Browse the repository at this point in the history
2.选择相册时丢失已选的问题
3.photos框架的滚动问题
  • Loading branch information
luowenxing committed Jan 26, 2018
1 parent c3e557e commit df154cf
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 158 deletions.
4 changes: 3 additions & 1 deletion MTImagePicker/Demo/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class ViewController: UITableViewController,MTImagePickerControllerDelegate {
self.tableView.reloadData()
}


func imagePickerControllerDidCancel(picker: MTImagePickerController) {
print("cancel")
}

func btnPickTouch() {
// 不推荐的写法,此处为了简便所以这样实现。
Expand Down
2 changes: 1 addition & 1 deletion MTImagePicker/MTImagePicker/Common/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension ALAsset {
class func getAssetFromUrlSync(lib:ALAssetsLibrary,url:NSURL) -> ALAsset? {
let sema = DispatchSemaphore(value: 0)
var result:ALAsset?
DispatchQueue.global(priority: .default).async {
DispatchQueue.global().async {
lib.asset(for: url as URL!, resultBlock: { (asset) in
result = asset
sema.signal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ class MTImagePickerAlbumCell:UITableViewCell {

class MTImagePickerAlbumsController :UITableViewController {

var mediaTypes:[MTImagePickerMediaType] = [MTImagePickerMediaType.Photo]
var source:MTImagePickerSource = .ALAsset
var maxCount:Int = Int.max
weak var delegate:MTImagePickerControllerDelegate?

weak var delegate:MTImagePickerDataSourceDelegate!
private var dataSource = [MTImagePickerAlbumModel]()
private var _source:MTImagePickerSource = .Photos

class var instance:MTImagePickerAlbumsController {
get {
Expand All @@ -42,7 +37,7 @@ class MTImagePickerAlbumsController :UITableViewController {

override func viewDidLoad() {
self.tableView.tableFooterView = UIView()
MTImagePickerDataSource.fetch(type: self.source, mediaTypes: self.mediaTypes, complete: { (dataSource) in
MTImagePickerDataSource.fetch(type: delegate.source, mediaTypes: delegate.mediaTypes, complete: { (dataSource) in
self.dataSource = dataSource
self.tableView.reloadData()
})
Expand All @@ -69,13 +64,11 @@ class MTImagePickerAlbumsController :UITableViewController {
func pushToMTImagePickerController(model:MTImagePickerAlbumModel,animate:Bool) {
let controller = MTImagePickerAssetsController.instance
controller.groupModel = model
controller.delegate = self.delegate
controller.maxCount = self.maxCount
controller.source = self.source
controller.delegate = delegate
self.navigationController?.pushViewController(controller, animated: animate)
}
@IBAction func btnCancelTouch(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
self.delegate.didCancel()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,15 @@ import Photos

class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {

weak var delegate:MTImagePickerControllerDelegate?
var maxCount: Int = Int.max
weak var delegate:MTImagePickerDataSourceDelegate!
var groupModel:MTImagePickerAlbumModel!
var source:MTImagePickerSource = .ALAsset

@IBOutlet weak var collectionView: MTImagePickerCollectionView!
@IBOutlet weak var lbSelected: UILabel!
@IBOutlet weak var btnPreview: UIButton!

private var dataSource = [MTImagePickerModel]()
private var selectedSource = Set<MTImagePickerModel>()
private var initialScrollDone:Bool = false
private var navigation:MTImagePickerController {
get {
return self.navigationController as! MTImagePickerController
}
}

class var instance:MTImagePickerAssetsController {
get {
Expand All @@ -44,26 +36,24 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
//MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
let loading = LoadingViewController()
loading.show(text: "Loading...".localized)
if let title = self.groupModel.getAlbumName() {
self.title = title
}
let loading = LoadingViewController()
loading.show(text: "Loading...".localized)
self.groupModel?.getMTImagePickerModelsListAsync { (models) in
loading.dismiss()
self.dataSource = models
self.collectionView.reloadData()
self.scrollToBottom()
}

self.initUI()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.collectionView.reloadData()
self.lbSelected.text = String(self.selectedSource.count)
self.btnPreview.isEnabled = !(self.selectedSource.count == 0)
self.lbSelected.text = String(delegate.selectedSource.count)
self.btnPreview.isEnabled = !(delegate.selectedSource.count == 0)
}

override func viewDidLayoutSubviews() {
Expand Down Expand Up @@ -97,7 +87,7 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
}
cell.imageView.image = model.getThumbImage(size: cell.imageView.frame.size)
cell.indexPath = indexPath
cell.btnCheck.isSelected = self.selectedSource.contains(model)
cell.btnCheck.isSelected = delegate.selectedSource.contains(model)
cell.btnCheck.addTarget(self, action: #selector(MTImagePickerAssetsController.btnCheckTouch(_:)), for: .touchUpInside)
cell.leading.constant = self.collectionView.leading.constant
cell.trailing.constant = self.collectionView.leading.constant
Expand All @@ -111,31 +101,26 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
}

@objc func btnCheckTouch(_ sender:UIButton) {
if self.selectedSource.count < self.maxCount || sender.isSelected == true {
if delegate.selectedSource.count < delegate.maxCount || sender.isSelected == true {
sender.isSelected = !sender.isSelected
let indexPath = (sender.superview?.superview as! MTImagePickerCell).indexPath
let index = (sender.superview?.superview as! MTImagePickerCell).indexPath.row
if sender.isSelected {
self.selectedSource.insert(self.dataSource[(indexPath?.row)!])
delegate.selectedSource.append(self.dataSource[index])
sender.heartbeatsAnimation(duration: 0.15)
}else {
self.selectedSource.remove(self.dataSource[(indexPath?.row)!])
if let removeIndex = delegate.selectedSource.index(of: self.dataSource[index]) {
delegate.selectedSource.remove(at: removeIndex)
}
}
self.lbSelected.text = String(self.selectedSource.count)
self.lbSelected.text = String(delegate.selectedSource.count)
self.lbSelected.heartbeatsAnimation(duration: 0.15)
self.btnPreview.isEnabled = !(self.selectedSource.count == 0)
self.btnPreview.isEnabled = !(delegate.selectedSource.count == 0)
} else {
let alertView = FlashAlertView(message: "Maxium selected".localized, delegate: nil)
alertView.show()
}
}

func showUnAuthorize() {
DispatchQueue.main.async {
let alertView = UIAlertView(title: "Notice".localized, message: "照片访问权限被禁用,请前往系统设置->隐私->照片中,启用本程序对照片的访问权限", delegate: nil, cancelButtonTitle: "OK".localized)
alertView.show()
}
}

//旋转处理
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
if self.interfaceOrientation.isPortrait != toInterfaceOrientation.isPortrait {
Expand All @@ -154,54 +139,25 @@ class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource
}
}

private func initUI() {
self.title = "All Photos".localized
}

private func pushToImageSelectorPreviewController(initialIndexPath:IndexPath?,dataSource:[MTImagePickerModel]) {
let vc = MTImagePickerPreviewController.instance
vc.dataSource = dataSource
vc.selectedSource = self.selectedSource
vc.delegate = self.delegate
vc.initialIndexPath = initialIndexPath
vc.maxCount = self.maxCount
vc.dismiss = {
selectedSource in
self.selectedSource = selectedSource
self.collectionView.reloadData()
}
self.navigationController?.pushViewController(vc, animated: true)
}

private func getSelectedSortedSource() -> [MTImagePickerModel] {
var dataSource = [MTImagePickerModel]()
for model in self.selectedSource.sorted(by: { return $0.sortNumber < $1.sortNumber}) {
dataSource.append(model)
}
return dataSource
}

//MARK: IBActions
@IBAction func btnFinishTouch(_ sender: AnyObject) {
let dataSource = self.getSelectedSortedSource()
if self.source == .Photos {
if #available(iOS 8.0, *) {
self.delegate?.imagePickerController?(picker:self.navigation, didFinishPickingWithPhotosModels: dataSource as! [MTImagePickerPhotosModel])
} else {
// Fallback on earlier versions
}
} else {
self.delegate?.imagePickerController?(picker:self.navigation, didFinishPickingWithAssetsModels: dataSource as! [MTImagePickerAssetsModel])
}
self.dismiss(animated: true, completion: nil)
delegate.didFinishPicking()
}

@IBAction func btnPreviewTouch(_ sender: AnyObject) {
let dataSource = self.getSelectedSortedSource()
let dataSource = delegate.selectedSource
self.pushToImageSelectorPreviewController(initialIndexPath: nil, dataSource: dataSource)
}
@IBAction func btnCancelTouch(_ sender: AnyObject) {
self.delegate?.imagePickerControllerDidCancel?(picker: self.navigation)
self.dismiss(animated: true, completion: nil)
delegate.didCancel()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,69 +25,37 @@ import UIKit
@objc optional func imagePickerControllerDidCancel(picker: MTImagePickerController)
}

protocol MTImagePickerDataSourceDelegate:NSObjectProtocol {
var selectedSource:[MTImagePickerModel] { get set }
var maxCount:Int { get }
var mediaTypes:[MTImagePickerMediaType] { get }
var source:MTImagePickerSource { get }
func didFinishPicking()
func didCancel()
}

public class MTImagePickerController:UINavigationController {

public weak var imagePickerDelegate:MTImagePickerControllerDelegate? {
get {
return self._delegate
}
set {
self._delegate = newValue
self.albumController?.delegate = newValue
}
}

public var mediaTypes:[MTImagePickerMediaType] {
get {
return self._mediaTypes
}
set {
self._mediaTypes.removeAll()
if newValue.contains(.Photo) {
self._mediaTypes.append(.Photo)
}
if newValue.contains(.Video) {
self._mediaTypes.append(.Video)
}
self.albumController?.mediaTypes = self._mediaTypes
}
}

public var maxCount: Int {
get {
return self._maxCount
}
set {
if newValue > 0 {
self._maxCount = newValue
self.albumController?.maxCount = newValue
}
}
}

public var defaultShowCameraRoll:Bool {
get {
return self._defaultAll
}
set {
self._defaultAll = newValue
}
}
public weak var imagePickerDelegate:MTImagePickerControllerDelegate?
public var mediaTypes:[MTImagePickerMediaType] = [.Photo]
public var maxCount: Int = Int.max
public var defaultShowCameraRoll:Bool = true
public var selectedSource = [MTImagePickerModel]()
private var _source = MTImagePickerSource.ALAsset
public var source:MTImagePickerSource {
get {
return self._source
}
set {
self._source = newValue
// 只有iOS8以上才能使用Photos框架
if newValue == .Photos {
if #available(iOS 8.0, *) {

} else {
self._source = .ALAsset
}
}
self.albumController?.source = self._source
}
}

Expand Down Expand Up @@ -116,30 +84,44 @@ public class MTImagePickerController:UINavigationController {
public override func viewWillAppear(_ animated: Bool) {
if self.defaultShowCameraRoll {
let controller = MTImagePickerAssetsController.instance
controller.delegate = self.imagePickerDelegate
controller.maxCount = self.maxCount
controller.source = self.source
controller.delegate = self
MTImagePickerDataSource.fetchDefault(type: self.source, mediaTypes: self.mediaTypes) {
controller.groupModel = $0
self.pushViewController(controller, animated: false)
}
}
}

class var instance:MTImagePickerController {
get {
let controller = MTImagePickerAlbumsController.instance
let navigation = MTImagePickerController(rootViewController: controller)
navigation.albumController = controller
controller.delegate = navigation
return navigation
}
}
}

public weak var _delegate:MTImagePickerControllerDelegate?
private var _mediaTypes = [MTImagePickerMediaType.Photo]
private var _maxCount:Int = Int.max
private var _defaultAll:Bool = true
private var _source = MTImagePickerSource.ALAsset
private weak var albumController:MTImagePickerAlbumsController?
extension MTImagePickerController:MTImagePickerDataSourceDelegate {

func didFinishPicking() {
if self.source == .Photos {
if #available(iOS 8.0, *) {
self.imagePickerDelegate?.imagePickerController?(picker:self, didFinishPickingWithPhotosModels: selectedSource as! [MTImagePickerPhotosModel])
} else {
// Fallback on earlier versions
}
} else {
self.imagePickerDelegate?.imagePickerController?(picker:self, didFinishPickingWithAssetsModels: selectedSource as! [MTImagePickerAssetsModel])
}
self.dismiss(animated: true, completion: nil)
}

func didCancel() {
imagePickerDelegate?.imagePickerControllerDidCancel?(picker: self)
self.dismiss(animated: true, completion: nil)
}

}


Loading

0 comments on commit df154cf

Please sign in to comment.