From e223801bc3f4ab26b8ba686f8f5d4f80d47880fe Mon Sep 17 00:00:00 2001 From: Den Date: Thu, 10 Sep 2020 17:04:36 +0300 Subject: [PATCH 1/2] add crop function for multiple photo mode --- Source/Filters/Crop/YPCropVC.swift | 3 ++ .../YPSelectionsGalleryVC.swift | 45 ++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Source/Filters/Crop/YPCropVC.swift b/Source/Filters/Crop/YPCropVC.swift index 68f1cfdf2..1b2c817e9 100644 --- a/Source/Filters/Crop/YPCropVC.swift +++ b/Source/Filters/Crop/YPCropVC.swift @@ -98,6 +98,9 @@ class YPCropVC: UIViewController { let imageRef = cgImage.cropping(to: scaledCropRect) { let croppedImage = UIImage(cgImage: imageRef) didFinishCropping?(croppedImage) + if YPConfig.library.maxNumberOfItems > 1 { + navigationController?.popViewController(animated: true) + } } } } diff --git a/Source/SelectionsGallery/YPSelectionsGalleryVC.swift b/Source/SelectionsGallery/YPSelectionsGalleryVC.swift index 0dd7fc379..3b1142f2c 100644 --- a/Source/SelectionsGallery/YPSelectionsGalleryVC.swift +++ b/Source/SelectionsGallery/YPSelectionsGalleryVC.swift @@ -18,7 +18,7 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel var v = YPSelectionsGalleryView() public override func loadView() { view = v } - + public required init(items: [YPMediaItem], didFinishHandler: @escaping ((_ gallery: YPSelectionsGalleryVC, _ items: [YPMediaItem]) -> Void)) { @@ -33,7 +33,7 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel override public func viewDidLoad() { super.viewDidLoad() - + // Register collection view cell v.collectionView.register(YPSelectionsGalleryCell.self, forCellWithReuseIdentifier: "item") v.collectionView.dataSource = self @@ -41,7 +41,7 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel // Setup navigation bar navigationItem.rightBarButtonItem = UIBarButtonItem(title: YPConfig.wordings.next, - style: .done, + style: .plain, target: self, action: #selector(done)) navigationItem.rightBarButtonItem?.tintColor = YPConfig.colors.tintColor @@ -52,7 +52,7 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel YPHelper.changeBackButtonIcon(self) YPHelper.changeBackButtonTitle(self) } - + @objc private func done() { // Save new images to the photo album. @@ -86,14 +86,18 @@ extension YPSelectionsGalleryVC: UICollectionViewDataSource { cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as? YPSelectionsGalleryCell else { - return UICollectionViewCell() + return UICollectionViewCell() } cell.delegate = self let item = items[indexPath.row] switch item { case .photo(let photo): cell.imageView.image = photo.image - cell.setEditable(YPConfig.showsPhotoFilters) + var showCrop = false + if case YPCropType.rectangle(_) = YPConfig.showsCrop { + showCrop = true + } + cell.setEditable(YPConfig.showsPhotoFilters || showCrop) case .video(let video): cell.imageView.image = video.thumbnail cell.setEditable(YPConfig.showsVideoTrimmer) @@ -112,6 +116,35 @@ extension YPSelectionsGalleryVC: UICollectionViewDelegate { case .photo(let photo): if !YPConfig.filters.isEmpty, YPConfig.showsPhotoFilters { mediaFilterVC = YPPhotoFiltersVC(inputPhoto: photo, isFromSelectionVC: true) + } else { + let completion = { (photo: YPMediaPhoto) in + let mediaItem = YPMediaItem.photo(p: photo) + // Save new image or existing but modified, to the photo album. + if YPConfig.shouldSaveNewPicturesToAlbum { + let isModified = photo.modifiedImage != nil + if photo.fromCamera || (!photo.fromCamera && isModified) { + YPPhotoSaver.trySaveImage(photo.image, inAlbumNamed: YPConfig.albumName) + } + } + self.items[indexPath.row] = mediaItem + collectionView.reloadData() + } + + func showCropVC(photo: YPMediaPhoto, completion: @escaping (_ aphoto: YPMediaPhoto) -> Void) { + if case let YPCropType.rectangle(ratio) = YPConfig.showsCrop { + let cropVC = YPCropVC(image: photo.originalImage, ratio: ratio) + cropVC.didFinishCropping = { croppedImage in + photo.modifiedImage = croppedImage + completion(photo) + } + self.show(cropVC, sender: self) + } else { + completion(photo) + } + } + + showCropVC(photo: photo, completion: completion) + return } case .video(let video): if YPConfig.showsVideoTrimmer { From 35de7af1ddca1ac3bfb49341b1df137c4759d8c5 Mon Sep 17 00:00:00 2001 From: Den Date: Thu, 4 Mar 2021 13:18:12 +0200 Subject: [PATCH 2/2] add possibility to customise navigation title of YPSelectionsGalleryVC --- Source/Configuration/YPImagePickerConfiguration.swift | 3 +++ Source/SelectionsGallery/YPSelectionsGalleryVC.swift | 1 + 2 files changed, 4 insertions(+) diff --git a/Source/Configuration/YPImagePickerConfiguration.swift b/Source/Configuration/YPImagePickerConfiguration.swift index 7679afd09..c5aecde6e 100644 --- a/Source/Configuration/YPImagePickerConfiguration.swift +++ b/Source/Configuration/YPImagePickerConfiguration.swift @@ -215,6 +215,9 @@ public struct YPConfigLibrary { /// Allow to skip the selections gallery when selecting the multiple media items. Defaults to false. public var skipSelectionsGallery = false + /// Selection gallery navigationItem title. Defaults to "". + public var selectionsGalleryTitle = "" + /// Allow to preselected media items public var preselectedItems: [YPMediaItem]? diff --git a/Source/SelectionsGallery/YPSelectionsGalleryVC.swift b/Source/SelectionsGallery/YPSelectionsGalleryVC.swift index 3b1142f2c..719a70cf8 100644 --- a/Source/SelectionsGallery/YPSelectionsGalleryVC.swift +++ b/Source/SelectionsGallery/YPSelectionsGalleryVC.swift @@ -40,6 +40,7 @@ public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDel v.collectionView.delegate = self // Setup navigation bar + navigationItem.title = YPConfig.library.selectionsGalleryTitle navigationItem.rightBarButtonItem = UIBarButtonItem(title: YPConfig.wordings.next, style: .plain, target: self,