Skip to content

Commit

Permalink
optimal performance
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyuexit committed Oct 30, 2017
1 parent 74847c4 commit fc83b0e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
24 changes: 14 additions & 10 deletions LPAlbum/Controllers/LPAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public class LPAlbum: UIViewController {

fileprivate var currentAlbumIndex: Int = 0
fileprivate var albumModels = [AlbumModel]()
fileprivate var selectedAssets = [PHAsset]()
fileprivate var allAssets = [PHAsset]() {
didSet{
//如果有Observer, 图片添加或者删除了, 要响应的添加或删除该图片的缓存
let itemSize = (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize
let itemSize = (self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize
let scale = UIScreen.main.scale
let targetSize = CGSize(width: itemSize.width * scale, height: itemSize.height * scale)
AlbumManager.imageManager.startCachingImages(for: allAssets, targetSize: targetSize, contentMode: .aspectFill, options: nil)
AlbumManager.imageManager.startCachingImages(for: self.allAssets, targetSize: targetSize, contentMode: .aspectFill, options: nil)

}
}

Expand Down Expand Up @@ -167,9 +169,8 @@ extension LPAlbum {

func cancel() { dismiss(animated: true, completion: nil) }
func confirm() {
let assets = albumModels.allSelectedAsset
var result = [UIImage]()
for asset in assets {
for asset in selectedAssets {
let size = CGSize(width: asset.pixelWidth, height: asset.pixelHeight) //PHImageManagerMaximumSize
let targetSize = targetSizeBlock?(size) ?? size
let option = PHImageRequestOptions()
Expand All @@ -183,7 +184,7 @@ extension LPAlbum {
contentMode: .aspectFit,
resultHandler: {[weak self] (image, _) in
if image != nil { result.append(image!) }
if result.count == assets.count {
if result.count == self?.selectedAssets.count {
DispatchQueue.main.async { self?.completeSelectImagesBlock?(result) }
}
})
Expand All @@ -201,7 +202,8 @@ extension LPAlbum {
guard let `self` = self else { return }

let willselect = !button.isSelected
guard self.checkoutMaxCount(willselect: willselect, show: vc) else { return }
let asset = vc.assetModels[index].asset
guard self.checkoutCount(willselect: willselect, asset: asset, show: vc) else { return }

button.isSelected = willselect
vc.assetModels[index].isSelect = willselect
Expand All @@ -227,7 +229,7 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {
cell.set(model)
cell.iconClickAction = {[weak self] in
guard let `self` = self else { return }
guard self.checkoutMaxCount(willselect: !$0, show: self) else { return }
guard self.checkoutCount(willselect: !$0, asset: model.asset, show: self) else { return }
var newModel = model
newModel.isSelect = !$0
self.albumModels = self.albumModels.change(assetModel: newModel)
Expand All @@ -248,7 +250,7 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if config.hasCamera && indexPath.row == 0 {
guard checkoutMaxCount(willselect: true, show: self) else { return }
guard checkoutCount(willselect: true, show: self) else { return }
AuthorizationTool.cameraRequestAuthorization{
$0 == .authorized ? self.takePhoto() : self.errorBlock?(self, AlbumError.noCameraPermission)
}
Expand All @@ -268,11 +270,13 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {
present(picker, animated: true, completion: nil)
}

func checkoutMaxCount(willselect: Bool, show vc: UIViewController) -> Bool {
if self.config.maxSelectCount == self.albumModels[0].selectCount && willselect {
func checkoutCount(willselect: Bool, asset: PHAsset? = nil, show vc: UIViewController) -> Bool {
if self.config.maxSelectCount == self.selectedAssets.count && willselect {
self.errorBlock?(vc,AlbumError.moreThanLargestChoiceCount)
return false
}
guard let asset = asset else { return true }
if willselect { self.selectedAssets.append(asset) } else { _ = self.selectedAssets.remove(asset) }
return true
}
}
Expand Down
10 changes: 6 additions & 4 deletions LPAlbum/Controllers/PhotosBrowerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ extension PhotosBrowerController {

func addCache() {
let scale = UIScreen.main.scale
AlbumManager.imageManager.startCachingImages(for: assetModels.map{ $0.asset },
targetSize: CGSize(width: itemSize.width * scale, height: itemSize.height * scale),
contentMode: .aspectFill,
options: nil)
DispatchQueue.global().async {
AlbumManager.imageManager.startCachingImages(for: self.assetModels.map{ $0.asset },
targetSize: CGSize(width: self.itemSize.width * scale, height: self.itemSize.height * scale),
contentMode: .aspectFill,
options: nil)
}
}
func removeCache() {
let scale = UIScreen.main.scale
Expand Down
2 changes: 2 additions & 0 deletions LPAlbum/Others/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public extension LPAlbum {
public static var normalBox: UIImage = Bundle.imageFromBundle("circle_normal")!
/// 选中的选择框图片
public static var selectedBox: UIImage = Bundle.imageFromBundle("circle_selected")!
/// 选择框box的可点击区域向外的扩展size
public static var boxEdgeInsets = UIEdgeInsets(top: -5, left: -5, bottom: -5, right: -5)
}

}
Expand Down
11 changes: 11 additions & 0 deletions LPAlbum/Others/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ extension Array where Iterator.Element == AlbumModel {
}
}

extension Array where Iterator.Element == PHAsset {
@discardableResult
mutating func remove(_ element: PHAsset) -> PHAsset? {
guard let index = index(of: element) else { return nil}
return remove(at: index)
}
}







Expand Down
2 changes: 1 addition & 1 deletion LPAlbum/Views/AlbumCollectionCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AlbumCollectionCell: UICollectionViewCell {
iconButton.frame = CGRect(x: frame.width - padding - iconWH, y: padding, width: iconWH, height: iconWH)
iconButton.setBackgroundImage(LPAlbum.Style.normalBox , for: .normal)
iconButton.setBackgroundImage(LPAlbum.Style.selectedBox, for: .selected)
iconButton.lp_hitEdgeInsets = UIEdgeInsets(top: -5, left: -5, bottom: -5, right: -5)
iconButton.lp_hitEdgeInsets = LPAlbum.Style.boxEdgeInsets

addSubview(photoView)
addSubview(iconButton)
Expand Down

0 comments on commit fc83b0e

Please sign in to comment.