Skip to content

Commit

Permalink
complete image cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyuexit committed Sep 15, 2017
1 parent d0f9c02 commit 574919c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 13 additions & 4 deletions LPAlbum/Controllers/LPAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
/**
TODO: 1. add CompleteSelectAssetsBlock
2. add Photo Cache
3. 文本国际化
4. 图片详情展示
4. 图片详情zoom
5. 选取单个图片的情形
6. 支持裁剪(正方形, 圆形)
7. take photo statusBarAnimation
8. 是否要加Observer
8. 是否要加Observer, 如果有Observer, 图片添加或者删除了, 要响应的添加或删除该图片的缓存
9. 完善demo(照片墙, 权限跳转)
10. 加一些动画效果
11. 添加注释
Expand Down Expand Up @@ -41,7 +40,15 @@ public class LPAlbum: UIViewController {

fileprivate var currentAlbumIndex: Int = 0
fileprivate var albumModels = [AlbumModel]()

fileprivate var allAssets = [PHAsset]() {
didSet{
//如果有Observer, 图片添加或者删除了, 要响应的添加或删除该图片的缓存
let itemSize = (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)
}
}

@discardableResult
public class func show(at: UIViewController, set: SetConfigBlock? = nil) -> LPAlbum {
Expand Down Expand Up @@ -93,6 +100,8 @@ public class LPAlbum: UIViewController {

albumModels = AlbumManager.getAlbums()
setupUI()
allAssets = albumModels[0].assetModels.map{ $0.asset }

titleView.clickAction = { [weak self] in
$0.isSelected = !$0.isSelected
$0.isSelected ?
Expand Down
24 changes: 22 additions & 2 deletions LPAlbum/Controllers/PhotosPreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ class PhotosPreviewController: UIViewController {
fileprivate var collectionView: UICollectionView!
fileprivate let itemPadding: CGFloat = 20.0
fileprivate let chooseButton = UIButton()
fileprivate var itemSize: CGSize!

override func viewDidLoad() {
super.viewDidLoad()
setupUI()
addCache()
}

deinit {
print("\(self) deinit")
removeCache()
}
}

Expand All @@ -42,7 +46,7 @@ extension PhotosPreviewController {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: chooseButton)

let layout = UICollectionViewFlowLayout()
let itemSize = CGSize(width: view.bounds.width + 20, height: view.bounds.height - 64)
itemSize = CGSize(width: view.bounds.width + 20, height: view.bounds.height - 64)
layout.itemSize = itemSize
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
Expand All @@ -56,9 +60,25 @@ extension PhotosPreviewController {
view.addSubview(collectionView)
collectionView.register(PhotoPreviewCell.self, forCellWithReuseIdentifier: PhotoPreviewCell.description())
collectionView.performBatchUpdates(nil){ _ in
self.collectionView.setContentOffset(CGPoint(x: itemSize.width * self.currentIndex.cgFloat, y: 0), animated: false)
self.collectionView.setContentOffset(CGPoint(x: self.itemSize.width * self.currentIndex.cgFloat, y: 0), animated: false)
}
}

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)
}
func removeCache() {
let scale = UIScreen.main.scale
AlbumManager.imageManager.stopCachingImages(for: assetModels.map{ $0.asset },
targetSize: CGSize(width: itemSize.width * scale, height: itemSize.height * scale),
contentMode: .aspectFill,
options: nil)
}

func chooseClick() {
chooseAction?(currentIndex, chooseButton, self)
}
Expand Down

0 comments on commit 574919c

Please sign in to comment.