-
Notifications
You must be signed in to change notification settings - Fork 205
/
RawImageViewController.swift
154 lines (137 loc) · 5.87 KB
/
RawImageViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// RawImageViewController.swift
// Example
//
// Created by JiongXing on 2019/11/29.
// Copyright © 2019 JiongXing. All rights reserved.
//
import UIKit
import JXPhotoBrowser
import SDWebImage
class RawImageViewController: BaseCollectionViewController {
override class func name() -> String { "显示查看原图" }
override class func remark() -> String { "举例如何实现查看原图" }
override func makeDataSource() -> [ResourceModel] {
let models = makeNetworkDataSource()
models[3].thirdLevelUrl = "https://github.com/JiongXing/PhotoBrowser/raw/master/Assets/rawImage.jpg"
return models
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.jx.dequeueReusableCell(BaseCollectionViewCell.self, for: indexPath)
if let firstLevel = self.dataSource[indexPath.item].firstLevelUrl {
let url = URL(string: firstLevel)
cell.imageView.sd_setImage(with: url, completed: nil)
}
return cell
}
override func openPhotoBrowser(with collectionView: UICollectionView, indexPath: IndexPath) {
let browser = JXPhotoBrowser()
browser.numberOfItems = {
self.dataSource.count
}
// 使用自定义的Cell
browser.cellClassAtIndex = { _ in
RawImageCell.self
}
browser.reloadCellAtIndex = { context in
let browserCell = context.cell as? RawImageCell
let collectionPath = IndexPath(item: context.index, section: indexPath.section)
let collectionCell = collectionView.cellForItem(at: collectionPath) as? BaseCollectionViewCell
let placeholder = collectionCell?.imageView.image
let urlString = self.dataSource[context.index].secondLevelUrl
let rawURLString = self.dataSource[context.index].thirdLevelUrl
browserCell?.reloadData(placeholder: placeholder, urlString: urlString, rawURLString: rawURLString)
}
browser.pageIndex = indexPath.item
browser.show()
}
}
/// 加上进度环的Cell
class RawImageCell: JXPhotoBrowserImageCell {
/// 进度环
let progressView = JXPhotoBrowserProgressView()
/// 查看原图按钮
var rawButton: UIButton = {
let button = UIButton(type: .custom)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.white, for: .highlighted)
button.backgroundColor = UIColor.black.withAlphaComponent(0.08)
button.setTitle("查看原图", for: .normal)
button.setTitle("查看原图", for: .highlighted)
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
button.layer.borderColor = UIColor.white.cgColor
button.layer.borderWidth = 1 / UIScreen.main.scale
button.layer.cornerRadius = 4
button.layer.masksToBounds = true
return button
}()
override func setup() {
super.setup()
addSubview(progressView)
rawButton.addTarget(self, action: #selector(onRawImageButton), for: .touchUpInside)
rawButton.isHidden = true
addSubview(rawButton)
}
override func layoutSubviews() {
super.layoutSubviews()
progressView.center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
rawButton.sizeToFit()
rawButton.bounds.size.width += 14
rawButton.center = CGPoint(x: bounds.width / 2,
y: bounds.height - 35 - rawButton.bounds.height)
}
func reloadData(placeholder: UIImage?, urlString: String?, rawURLString: String?) {
self.rawURLString = rawURLString
progressView.progress = 0
rawButton.isHidden = true
let url = urlString.flatMap { URL(string: $0) }
imageView.sd_setImage(with: url, placeholderImage: placeholder, options: []) { [weak self] (_, error, _, _) in
if error == nil {
self?.setNeedsLayout()
}
// 原图如果有缓存就加载
self?.loadRawImageIfCached(placeholder: self?.imageView.image, rawURLString: rawURLString)
}
}
/// 尝试加载缓存的原图,completion回调传参:true-已加载缓存,false-没有缓存
private func loadRawImageIfCached(placeholder: UIImage?, rawURLString: String?) {
guard let rawString = rawURLString else {
return
}
SDImageCache.shared.containsImage(forKey: rawString, cacheType: .all) { [weak self] cacheType in
if cacheType == .none {
self?.rawButton.isHidden = false
return
}
self?.imageView.sd_setImage(with: URL(string: rawString), placeholderImage: placeholder, options: []) { (_, error, _, _) in
if error == nil {
self?.setNeedsLayout()
}
self?.rawButton.isHidden = (error == nil)
}
}
}
/// 保存原图url
private var rawURLString: String?
/// 响应查看原图按钮
@objc func onRawImageButton(_ button: UIButton) {
self.rawButton.isHidden = true
guard let url = rawURLString.flatMap({ URL(string: $0) }) else {
progressView.isHidden = true
return
}
imageView.sd_setImage(with: url, placeholderImage: imageView.image, options: [], progress: { [weak self] (received, total, _) in
if total > 0 {
self?.progressView.progress = CGFloat(received) / CGFloat(total)
}
}) { [weak self] (_, error, _, _) in
if error == nil {
self?.progressView.progress = 1.0
self?.setNeedsLayout()
} else {
self?.progressView.progress = 0
self?.rawButton.isHidden = false
}
}
}
}