|
9 | 9 | import UIKit |
10 | 10 | import Photos |
11 | 11 |
|
12 | | -@objc protocol ImagePickerCollectionViewDelegate { |
13 | | - @objc optional func didSelectImage(with localIdentifer: String) |
| 12 | +/// Image picker result |
| 13 | +@objc public protocol ImagePickerResultDelegate { |
| 14 | + @objc optional func didSelectImage(url: URL?) |
| 15 | + @objc optional func didSelectVideo(url: URL?) |
14 | 16 | } |
15 | 17 |
|
16 | 18 | public final class ImagePickerCollectionView: UICollectionView { |
17 | 19 |
|
18 | 20 | var takePhoto: (() -> ())? |
19 | 21 | var showCollection: (() -> ())? |
20 | 22 |
|
21 | | - weak var pickerDelegate: ImagePickerCollectionViewDelegate? |
| 23 | + weak var pickerDelegate: ImagePickerResultDelegate? |
22 | 24 |
|
23 | 25 | fileprivate var nColumns = 1 |
24 | 26 | fileprivate var spacing: CGFloat = 2 |
@@ -120,11 +122,29 @@ extension ImagePickerCollectionView: UICollectionViewDataSource { |
120 | 122 | } |
121 | 123 |
|
122 | 124 | public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { |
123 | | - photoDataManager.requestImage(for: indexPath) { (image, indexPath) in |
124 | | - guard let image = image else { return } |
125 | | - guard let cell = collectionView.cellForItem(at: indexPath) as? ImagePickerCollectionCell else { return } |
126 | | - |
127 | | - cell.imageView.image = image |
| 125 | + guard let asset = photoDataManager.getAsset(for: indexPath) else { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + DispatchQueue.main.async { |
| 130 | + self.photoDataManager.requestImage(for: asset, at: indexPath) { (image, indexPath) in |
| 131 | + guard let image = image else { return } |
| 132 | + guard let cell = collectionView.cellForItem(at: indexPath) as? ImagePickerCollectionCell else { return } |
| 133 | + |
| 134 | + cell.imageView.image = image |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + switch asset.mediaType { |
| 139 | + case .video: |
| 140 | + DispatchQueue.main.async { |
| 141 | + MediaProcesser.getVideoLength(videoAsset: asset) { (length, error) in |
| 142 | + if let videoLength = length { |
| 143 | + print("Video length = \(videoLength)") |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + default: break |
128 | 148 | } |
129 | 149 | } |
130 | 150 |
|
@@ -152,10 +172,22 @@ extension ImagePickerCollectionView: UICollectionViewDelegateFlowLayout { |
152 | 172 |
|
153 | 173 | extension ImagePickerCollectionView: UICollectionViewDelegate { |
154 | 174 |
|
155 | | - private func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
156 | | - let localIdentifer = photoDataManager.photoIdentifier(for: indexPath.row) |
157 | | - |
158 | | - pickerDelegate?.didSelectImage?(with: localIdentifer) |
| 175 | + public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| 176 | + guard let asset = photoDataManager.getAsset(for: indexPath) else { |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + switch asset.mediaType { |
| 181 | + case .video: |
| 182 | + MediaProcesser.storeVideoToURL(videoAsset: asset) { [weak self] (url, error) in |
| 183 | + self?.pickerDelegate?.didSelectVideo?(url: url) |
| 184 | + } |
| 185 | + case .image: |
| 186 | + MediaProcesser.storeImage(imageAsset: asset) { [weak self] (url, error) in |
| 187 | + self?.pickerDelegate?.didSelectImage?(url: url) |
| 188 | + } |
| 189 | + default: break |
| 190 | + } |
159 | 191 | } |
160 | 192 |
|
161 | 193 | } |
@@ -187,13 +219,13 @@ extension ImagePickerCollectionView { |
187 | 219 |
|
188 | 220 | extension ImagePickerCollectionView: PhotoDataManagerDelegate { |
189 | 221 |
|
190 | | - func photoDataManagerDidUpdate() { |
| 222 | + public func photoDataManagerDidUpdate() { |
191 | 223 | performBatchUpdates({ [weak self] in |
192 | 224 | self?.reloadSections(IndexSet(integer: 0)) |
193 | 225 | }, completion: nil) |
194 | 226 | } |
195 | 227 |
|
196 | | - func targetSize(for indexPath: IndexPath) -> CGSize { |
| 228 | + public func targetSize(for indexPath: IndexPath) -> CGSize { |
197 | 229 | let cell = cellForItem(at: indexPath) |
198 | 230 | let height = cell?.bounds.size.height ?? bounds.height |
199 | 231 | return CGSize(width: height * UIScreen.main.scale, |
|
0 commit comments