Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a custom collection of TLPHAssets #68

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TLPhotoPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ TODO: Add long description of the pod here.
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
s.dependency 'SDWebImage'
end
63 changes: 52 additions & 11 deletions TLPhotoPicker/Classes/TLAssetsCollection.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Photos
import PhotosUI
import MobileCoreServices

public struct TLPHAsset {
public class TLPHAsset {

enum CloudDownloadState {
case ready, progress, complete, failed
}
Expand All @@ -32,21 +33,30 @@ public struct TLPHAsset {
guard let phAsset = self.phAsset else { return .photo }
if phAsset.mediaSubtypes.contains(.photoLive) {
return .livePhoto
}else if phAsset.mediaType == .video {
} else if phAsset.mediaType == .video {
return .video
}else {
} else {
return .photo
}
}
}

private var _fullResolutionImage: UIImage?
public var fullResolutionImage: UIImage? {
set {
_fullResolutionImage = newValue
}
get {
if let image = _fullResolutionImage {
return image
}
guard let phAsset = self.phAsset else { return nil }
return TLPhotoLibrary.fullResolutionImageData(asset: phAsset)
}
}

public var url: URL?

public func extType() -> ImageExtType {
var ext = ImageExtType.png
if let fileName = self.originalFileName, let extention = URL(string: fileName)?.pathExtension.lowercased() {
Expand All @@ -55,6 +65,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 +207,31 @@ public struct TLPHAsset {
init(asset: PHAsset?) {
self.phAsset = asset
}

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

public convenience init(url: URL) {
self.init(asset: nil)
self.url = url
}
}

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 +241,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 +255,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 +273,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, index < assets.count {
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
1 change: 1 addition & 0 deletions TLPhotoPicker/Classes/TLBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Foundation

class TLBundle {

class func podBundleImage(named: String) -> UIImage? {
let podBundle = Bundle(for: TLBundle.self)
if let url = podBundle.url(forResource: "TLPhotoPickerController", withExtension: "bundle") {
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
9 changes: 4 additions & 5 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 @@ -148,10 +148,10 @@ extension TLPhotoLibrary {
let fetchCollection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: subType, options: nil)
var collections = [PHAssetCollection]()
fetchCollection.enumerateObjects { (collection, index, _) in
//Why this? : Can't getting image for cloud shared album
if collection.assetCollectionSubtype != .albumCloudShared {
// //Why this? : Can't getting image for cloud shared album
// if collection.assetCollectionSubtype != .albumCloudShared {
collections.append(collection)
}
// }
}
for collection in collections {
if !result.contains(where: { $0.localIdentifier == collection.localIdentifier }) {
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