Skip to content

Commit

Permalink
- Fixed: video don't stop playing on the dismiss the controller
Browse files Browse the repository at this point in the history
- Added: allowedVideoRecording and maxVideoDuration options on configure
  • Loading branch information
Breno Lima committed Nov 3, 2017
1 parent 70ee382 commit bcabce0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
16 changes: 11 additions & 5 deletions TLPhotoPicker/Classes/TLPhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ extension TLPhotoLibrary {
return options
}

func fetchResult(collection: TLAssetsCollection?) -> PHFetchResult<PHAsset>? {
func fetchResult(collection: TLAssetsCollection?, maxVideoDuration:TimeInterval?=nil) -> PHFetchResult<PHAsset>? {
guard let phAssetCollection = collection?.phAssetCollection else { return nil }
return PHAsset.fetchAssets(in: phAssetCollection, options: getOption())
let options = getOption()
if let duration = maxVideoDuration, phAssetCollection.assetCollectionSubtype == .smartAlbumVideos {
options.predicate = NSPredicate(format: "mediaType = %i AND duration <= %f", PHAssetMediaType.video.rawValue, duration)
}
return PHAsset.fetchAssets(in: phAssetCollection, options: options)
}

func fetchCollection(allowedVideo: Bool = true, useCameraButton: Bool = true, mediaType: PHAssetMediaType? = nil) {
func fetchCollection(allowedVideo: Bool = true, useCameraButton: Bool = true, mediaType: PHAssetMediaType? = nil, maxVideoDuration:TimeInterval? = nil) {
let options = getOption()

@discardableResult
Expand All @@ -141,9 +145,11 @@ extension TLPhotoLibrary {
}

if let mediaType = mediaType {
options.predicate = NSPredicate(format: "mediaType = %i", mediaType.rawValue)
}else if !allowedVideo {
options.predicate = maxVideoDuration != nil && mediaType == PHAssetMediaType.video ? NSPredicate(format: "mediaType = %i AND duration <= %f", mediaType.rawValue, maxVideoDuration!) : NSPredicate(format: "mediaType = %i", mediaType.rawValue)
} else if !allowedVideo {
options.predicate = NSPredicate(format: "mediaType = %i", PHAssetMediaType.image.rawValue)
} else if let duration = maxVideoDuration {
options.predicate = NSPredicate(format: "mediaType = %i OR (mediaType = %i AND duration <= %f)", PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue, duration)
}

DispatchQueue.global(qos: .userInteractive).async { [weak self] in
Expand Down
73 changes: 40 additions & 33 deletions TLPhotoPicker/Classes/TLPhotosPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import UIKit
import Photos
import PhotosUI
import MobileCoreServices

public protocol TLPhotosPickerViewControllerDelegate: class {
func dismissPhotoPicker(withPHAssets: [PHAsset])
Expand All @@ -34,6 +35,8 @@ public struct TLPhotosPickerConfigure {
public var usedPrefetch = false
public var allowedLivePhotos = true
public var allowedVideo = true
public var allowedVideoRecording = true
public var maxVideoDuration:TimeInterval? = nil
public var autoPlay = true
public var muteAudio = false
public var mediaType: PHAssetMediaType? = nil
Expand All @@ -45,7 +48,6 @@ public struct TLPhotosPickerConfigure {
public var videoIcon = TLBundle.podBundleImage(named: "video")
public var placeholderIcon = TLBundle.podBundleImage(named: "insertPhotoMaterial")
public var nibSet: (nibName: String, bundle:Bundle)? = nil
public var cameraCellNibSet: (nibName: String, bundle:Bundle)? = nil
public init() {

}
Expand All @@ -54,7 +56,7 @@ public struct TLPhotosPickerConfigure {

public struct Platform {

public static var isSimulator: Bool {
static var isSimulator: Bool {
return TARGET_OS_SIMULATOR != 0 // Use this line in Xcode 7 or newer
}

Expand Down Expand Up @@ -220,9 +222,6 @@ extension TLPhotosPickerViewController {
if let nibSet = self.configure.nibSet {
registerNib(nibName: nibSet.nibName, bundle: nibSet.bundle)
}
if let nibSet = self.configure.cameraCellNibSet {
registerNib(nibName: nibSet.nibName, bundle: nibSet.bundle)
}
self.indicator.startAnimating()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(titleTap))
self.titleView.addGestureRecognizer(tapGesture)
Expand Down Expand Up @@ -273,7 +272,7 @@ extension TLPhotosPickerViewController {
fileprivate func initPhotoLibrary() {
if PHPhotoLibrary.authorizationStatus() == .authorized {
self.photoLibrary.delegate = self
self.photoLibrary.fetchCollection(allowedVideo: self.allowedVideo, useCameraButton: self.usedCameraButton, mediaType: self.configure.mediaType)
self.photoLibrary.fetchCollection(allowedVideo: self.allowedVideo, useCameraButton: self.usedCameraButton, mediaType: self.configure.mediaType, maxVideoDuration:self.configure.maxVideoDuration)
}else{
//self.dismiss(animated: true, completion: nil)
}
Expand All @@ -297,7 +296,7 @@ extension TLPhotosPickerViewController {
self.collections[getfocusedIndex()].recentPosition = self.collectionView.contentOffset
var reloadIndexPaths = [IndexPath(row: getfocusedIndex(), section: 0)]
self.focusedCollection = collection
self.focusedCollection?.fetchResult = self.photoLibrary.fetchResult(collection: collection)
self.focusedCollection?.fetchResult = self.photoLibrary.fetchResult(collection: collection, maxVideoDuration:self.configure.maxVideoDuration)
reloadIndexPaths.append(IndexPath(row: getfocusedIndex(), section: 0))
self.albumPopView.tableView.reloadRows(at: reloadIndexPaths, with: .none)
self.albumPopView.show(false, duration: 0.2)
Expand Down Expand Up @@ -368,10 +367,12 @@ extension TLPhotosPickerViewController {
}

@IBAction open func cancelButtonTap() {
self.stopPlay()
self.dismiss(done: false)
}

@IBAction open func doneButtonTap() {
self.stopPlay()
self.dismiss(done: true)
}

Expand Down Expand Up @@ -430,6 +431,13 @@ extension TLPhotosPickerViewController: UIImagePickerControllerDelegate, UINavig
guard !maxCheck() else { return }
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.mediaTypes = [kUTTypeImage as String]
if self.configure.allowedVideoRecording {
picker.mediaTypes.append(kUTTypeMovie as String)
if let duration = self.configure.maxVideoDuration {
picker.videoMaximumDuration = duration
}
}
picker.allowsEditing = false
picker.delegate = self
self.present(picker, animated: true, completion: nil)
Expand All @@ -454,6 +462,21 @@ extension TLPhotosPickerViewController: UIImagePickerControllerDelegate, UINavig
}
})
}
else if (info[UIImagePickerControllerMediaType] as? String) == kUTTypeMovie as String {
var placeholderAsset: PHObjectPlaceholder? = nil
PHPhotoLibrary.shared().performChanges({
let newAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: info[UIImagePickerControllerMediaURL] as! URL)
placeholderAsset = newAssetRequest?.placeholderForCreatedAsset
}) { [weak self] (sucess, error) in
if sucess, let `self` = self, let identifier = placeholderAsset?.localIdentifier {
guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [identifier], options: nil).firstObject else { return }
var result = TLPHAsset(asset: asset)
result.selectedOrder = self.selectedAssets.count + 1
self.selectedAssets.append(result)
}
}
}

picker.dismiss(animated: true, completion: nil)
}
}
Expand Down Expand Up @@ -592,21 +615,17 @@ extension TLPhotosPickerViewController: UICollectionViewDelegate,UICollectionVie

//Delegate
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let collection = self.focusedCollection,let cell = self.collectionView.cellForItem(at: indexPath) as? TLPhotoCollectionViewCell else { return }
guard let collection = self.focusedCollection else { return }
if collection.useCameraButton && indexPath.row == 0 {
if Platform.isSimulator {
print("not supported by the simulator.")
return
}else {
if let nibName = self.configure.cameraCellNibSet?.nibName {
cell.selectedCell()
}else {
showCamera()
}
showCamera()
return
}
}
guard var asset = collection.getTLAsset(at: indexPath.row) else { return }
guard var asset = collection.getTLAsset(at: indexPath.row), let cell = self.collectionView.cellForItem(at: indexPath) as? TLPhotoCollectionViewCell else { return }
cell.popScaleAnim()
if let index = self.selectedAssets.index(where: { $0.phAsset == asset.phAsset }) {
//deselect
Expand Down Expand Up @@ -637,12 +656,9 @@ extension TLPhotosPickerViewController: UICollectionViewDelegate,UICollectionVie
}

open func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? TLPhotoCollectionViewCell {
cell.endDisplayingCell()
if indexPath == self.playRequestId?.indexPath {
self.playRequestId = nil
cell.stopPlay()
}
if indexPath == self.playRequestId?.indexPath, let cell = cell as? TLPhotoCollectionViewCell {
self.playRequestId = nil
cell.stopPlay()
}
guard let requestId = self.requestIds[indexPath] else { return }
self.requestIds.removeValue(forKey: indexPath)
Expand All @@ -651,23 +667,14 @@ extension TLPhotosPickerViewController: UICollectionViewDelegate,UICollectionVie

//Datasource
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
func makeCell(nibName: String) -> TLPhotoCollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: nibName, for: indexPath) as! TLPhotoCollectionViewCell
cell.configure = self.configure
cell.imageView?.image = self.placeholderThumbnail
return cell
}
let nibName = self.configure.nibSet?.nibName ?? "TLPhotoCollectionViewCell"
var cell = makeCell(nibName: nibName)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: nibName, for: indexPath) as! TLPhotoCollectionViewCell
cell.configure = self.configure
cell.imageView?.image = self.placeholderThumbnail
guard let collection = self.focusedCollection else { return cell }
cell.isCameraCell = collection.useCameraButton && indexPath.row == 0
if cell.isCameraCell {
if let nibName = self.configure.cameraCellNibSet?.nibName {
cell = makeCell(nibName: nibName)
}else{
cell.imageView?.image = self.cameraImage
}
cell.willDisplayCell()
cell.imageView?.image = self.cameraImage
return cell
}
guard let asset = collection.getTLAsset(at: indexPath.row) else { return cell }
Expand Down

0 comments on commit bcabce0

Please sign in to comment.