Skip to content

Commit

Permalink
Support a custom collection of TLPHAssets
Browse files Browse the repository at this point in the history
To support displaying a collection which doesn't belong to the library.
Changed TLPHAsset from struct to class to become reference type.
Added localIdentifier to TLPHAsset for comparing.
Changed TLPhotosPickerViewControllerDelegate for complex callbacks.
Set picker's navigationBar.isTranslucent to false by default.
  • Loading branch information
CocoaBob committed Jan 20, 2018
1 parent 6ce54b5 commit f8636a4
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 59 deletions.
51 changes: 42 additions & 9 deletions TLPhotoPicker/Classes/TLAssetsCollection.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Photos
import PhotosUI
import MobileCoreServices

public struct TLPHAsset {
public class TLPHAsset {
enum CloudDownloadState {
case ready, progress, complete, failed
}
Expand Down Expand Up @@ -40,8 +40,15 @@ public struct TLPHAsset {
}
}

private var _fullResolutionImage: UIImage?
public var fullResolutionImage: UIImage? {
set {
_fullResolutionImage = newValue
}
get {
if let _ = _fullResolutionImage {
return _fullResolutionImage
}
guard let phAsset = self.phAsset else { return nil }
return TLPhotoLibrary.fullResolutionImageData(asset: phAsset)
}
Expand All @@ -55,6 +62,7 @@ public struct TLPHAsset {
return ext
}

public lazy var localIdentifier = UUID().uuidString
@discardableResult
public func cloudImageDownload(progressBlock: @escaping (Double) -> Void, completionBlock:@escaping (UIImage?)-> Void ) -> PHImageRequestID? {
guard let phAsset = self.phAsset else { return nil }
Expand Down Expand Up @@ -196,16 +204,26 @@ public struct TLPHAsset {
init(asset: PHAsset?) {
self.phAsset = asset
}

public convenience init(image: UIImage) {
self.init(asset: nil)
self.fullResolutionImage = image
}
}

extension TLPHAsset: Equatable {

public static func ==(lhs: TLPHAsset, rhs: TLPHAsset) -> Bool {
guard let lphAsset = lhs.phAsset, let rphAsset = rhs.phAsset else { return false }
return lphAsset.localIdentifier == rphAsset.localIdentifier
if let lphAsset = lhs.phAsset, let rphAsset = rhs.phAsset {
return lphAsset.localIdentifier == rphAsset.localIdentifier
} else {
return lhs.localIdentifier == rhs.localIdentifier
}
}
}

struct TLAssetsCollection {
public struct TLAssetsCollection {
var customAssets: [TLPHAsset]? = nil
var phAssetCollection: PHAssetCollection? = nil
var fetchResult: PHFetchResult<PHAsset>? = nil
var thumbnail: UIImage? = nil
Expand All @@ -215,8 +233,12 @@ struct TLAssetsCollection {
var localIdentifier: String
var count: Int {
get {
guard let count = self.fetchResult?.count, count > 0 else { return self.useCameraButton ? 1 : 0 }
return count + (self.useCameraButton ? 1 : 0)
if let assets = self.customAssets {
return assets.count
} else {
guard let count = self.fetchResult?.count, count > 0 else { return self.useCameraButton ? 1 : 0 }
return count + (self.useCameraButton ? 1 : 0)
}
}
}

Expand All @@ -225,7 +247,14 @@ struct TLAssetsCollection {
self.title = collection.localizedTitle ?? ""
self.localIdentifier = collection.localIdentifier
}


public init(assets: [TLPHAsset], title: String?) {
self.customAssets = assets
self.thumbnail = assets[0].fullResolutionImage
self.title = title ?? ""
self.localIdentifier = UUID().uuidString
}

func getAsset(at index: Int) -> PHAsset? {
if self.useCameraButton && index == 0 { return nil }
let index = index - (self.useCameraButton ? 1 : 0)
Expand All @@ -236,8 +265,12 @@ struct TLAssetsCollection {
func getTLAsset(at index: Int) -> TLPHAsset? {
if self.useCameraButton && index == 0 { return nil }
let index = index - (self.useCameraButton ? 1 : 0)
guard let result = self.fetchResult, index < result.count else { return nil }
return TLPHAsset(asset: result.object(at: max(index,0)))
if let assets = self.customAssets {
return assets[index]
} else {
guard let result = self.fetchResult, index < result.count else { return nil }
return TLPHAsset(asset: result.object(at: max(index,0)))
}
}

func getAssets(at range: CountableClosedRange<Int>) -> [PHAsset]? {
Expand Down
2 changes: 2 additions & 0 deletions TLPhotoPicker/Classes/TLPhotoCollectionViewCell.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ open class TLPhotoCollectionViewCell: UICollectionViewCell {
super.awakeFromNib()
self.playerView?.playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.livePhotoView?.isHidden = true
self.durationLabel?.isHidden = true
self.durationView?.isHidden = true
self.selectedView?.isHidden = true
self.selectedView?.layer.borderWidth = 10
Expand All @@ -156,6 +157,7 @@ open class TLPhotoCollectionViewCell: UICollectionViewCell {
override open func prepareForReuse() {
super.prepareForReuse()
self.livePhotoView?.isHidden = true
self.durationLabel?.isHidden = true
self.durationView?.isHidden = true
self.durationView?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
self.selectedHeight?.constant = 10
Expand Down
3 changes: 1 addition & 2 deletions TLPhotoPicker/Classes/TLPhotoLibrary.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Photos
protocol TLPhotoLibraryDelegate: class {
func loadCameraRollCollection(collection: TLAssetsCollection)
func loadCompleteAllCollection(collections: [TLAssetsCollection])
func focusCollection(collection: TLAssetsCollection)
func focusCollection(collection: TLAssetsCollection?)
}

class TLPhotoLibrary {
Expand Down Expand Up @@ -194,7 +194,6 @@ extension TLPhotoLibrary {
cameraRoll.useCameraButton = useCameraButton
assetCollections[0] = cameraRoll
DispatchQueue.main.async {
self?.delegate?.focusCollection(collection: cameraRoll)
self?.delegate?.loadCameraRollCollection(collection: cameraRoll)
}
}
Expand Down
Loading

0 comments on commit f8636a4

Please sign in to comment.