From a669169c26ac19115de753273c07e484315473a1 Mon Sep 17 00:00:00 2001 From: Nirali Shah Date: Thu, 10 Nov 2022 22:13:29 +0530 Subject: [PATCH] The compression and sorting option is completely configurable --- Example/ExampleViewController.swift | 3 + .../YPImagePickerConfiguration.swift | 74 +++++++++++++++++++ Source/Configuration/YPWordings.swift | 11 +++ .../Pages/Gallery/LibraryMediaManager.swift | 64 ---------------- Source/Pages/Gallery/YPLibraryVC.swift | 9 +-- 5 files changed, 92 insertions(+), 69 deletions(-) diff --git a/Example/ExampleViewController.swift b/Example/ExampleViewController.swift index 23e74becd..58b6f122e 100644 --- a/Example/ExampleViewController.swift +++ b/Example/ExampleViewController.swift @@ -89,6 +89,7 @@ class ExampleViewController: UIViewController { /* Choose what media types are available in the library. Defaults to `.photo` */ config.library.mediaType = .photoAndVideo config.library.itemOverlayType = .grid + config.library.sortingOption = .creationDate /* Enables selecting the front camera by default, useful for avatars. Defaults to false */ // config.usesFrontCamera = true @@ -133,6 +134,8 @@ class ExampleViewController: UIViewController { /* Defines the time limit for videos from the library. Defaults to 60 seconds. */ config.video.libraryTimeLimit = 500.0 + + config.video.compressionOption = [compressionOptions.AVAssetExportPreset640x480,compressionOptions.AVAssetExportPreset1920x1080, compressionOptions.AVAssetExportPresetPassthrough] /* Adds a Crop step in the photo taking process, after filters. Defaults to .none */ config.showsCrop = .rectangle(ratio: (16/9)) diff --git a/Source/Configuration/YPImagePickerConfiguration.swift b/Source/Configuration/YPImagePickerConfiguration.swift index 5cb79e13b..2188b73b6 100644 --- a/Source/Configuration/YPImagePickerConfiguration.swift +++ b/Source/Configuration/YPImagePickerConfiguration.swift @@ -227,6 +227,9 @@ public struct YPConfigLibrary { /// Set the overlay type shown on top of the selected library item public var itemOverlayType: YPItemOverlayType = .grid + + /// The library items are sorted by time created or modified + public var sortingOption: SortingOption = SortingOption.creationDate } /// Encapsulates video specific settings. @@ -246,6 +249,9 @@ public struct YPConfigVideo { */ public var compression: String = AVAssetExportPresetHighestQuality + /// Choose the different video compression option to be supported + public var compressionOption: [compressionOptions] = [compressionOptions.AVAssetExportPresetPassthrough] + /// Choose the result video extension if you trim or compress a video. Defaults to mov. public var fileType: AVFileType = .mov @@ -304,3 +310,71 @@ public enum YPlibraryMediaType { case video case photoAndVideo } + +public enum SortingOption: String { + case modificationDate = "modificationDate" + case creationDate = "creationDate" +} + +public enum compressionOptions: String { + case AVAssetExportPresetLowQuality + case AVAssetExportPreset640x480 + case AVAssetExportPresetMediumQuality + case AVAssetExportPreset1920x1080 + case AVAssetExportPreset1280x720 + case AVAssetExportPresetHighestQuality + case AVAssetExportPresetAppleM4A + case AVAssetExportPreset3840x2160 + case AVAssetExportPreset960x540 + case AVAssetExportPresetPassthrough + + func presetID() -> String { + switch self { + case .AVAssetExportPresetLowQuality: + return "AVAssetExportPresetLowQuality" + case .AVAssetExportPreset640x480: + return "AVAssetExportPreset640x480" + case .AVAssetExportPresetMediumQuality: + return "AVAssetExportPresetMediumQuality" + case .AVAssetExportPreset1920x1080: + return "AVAssetExportPreset1920x1080" + case .AVAssetExportPreset1280x720: + return "AVAssetExportPreset1280x720" + case .AVAssetExportPresetHighestQuality: + return "AVAssetExportPresetHighestQuality" + case .AVAssetExportPresetAppleM4A: + return "AVAssetExportPresetAppleM4A" + case .AVAssetExportPreset3840x2160: + return "AVAssetExportPreset3840x2160" + case .AVAssetExportPreset960x540: + return "AVAssetExportPreset960x540" + case .AVAssetExportPresetPassthrough: + return "AVAssetExportPresetPassthrough" + } + } + + func getLabel() -> String { + switch self { + case .AVAssetExportPresetLowQuality: + return YPConfig.wordings.textAVAssetExportPresetLowQuality + case .AVAssetExportPreset640x480: + return YPConfig.wordings.textAVAssetExportPreset640x480 + case .AVAssetExportPresetMediumQuality: + return YPConfig.wordings.textAVAssetExportPresetMediumQuality + case .AVAssetExportPreset1920x1080: + return YPConfig.wordings.textAVAssetExportPreset1920x1080 + case .AVAssetExportPreset1280x720: + return YPConfig.wordings.textAVAssetExportPreset1280x720 + case .AVAssetExportPresetHighestQuality: + return YPConfig.wordings.textAVAssetExportPresetHighestQuality + case .AVAssetExportPresetAppleM4A: + return YPConfig.wordings.textAVAssetExportPresetAppleM4A + case .AVAssetExportPreset3840x2160: + return YPConfig.wordings.textAVAssetExportPreset3840x2160 + case .AVAssetExportPreset960x540: + return YPConfig.wordings.textAVAssetExportPreset960x540 + case .AVAssetExportPresetPassthrough: + return YPConfig.wordings.textAVAssetExportPresetPassthrough + } + } +} diff --git a/Source/Configuration/YPWordings.swift b/Source/Configuration/YPWordings.swift index 2a4288961..eb01f614c 100644 --- a/Source/Configuration/YPWordings.swift +++ b/Source/Configuration/YPWordings.swift @@ -41,4 +41,15 @@ public struct YPWordings { public var filter = ypLocalized("YPImagePickerFilter") public var crop = ypLocalized("YPImagePickerCrop") public var warningMaxItemsLimit = ypLocalized("YPImagePickerWarningItemsLimit") + + public var textAVAssetExportPresetLowQuality = ypLocalized("Low") + public var textAVAssetExportPreset640x480 = ypLocalized("Medium (HD)") + public var textAVAssetExportPresetMediumQuality = ypLocalized("Medium") + public var textAVAssetExportPreset1920x1080 = ypLocalized("High (Full HD)") + public var textAVAssetExportPreset1280x720 = ypLocalized("1280 x 720") + public var textAVAssetExportPresetHighestQuality = ypLocalized("Highest") + public var textAVAssetExportPresetAppleM4A = ypLocalized("Apple M4A") + public var textAVAssetExportPreset3840x2160 = ypLocalized("3840 x 2160") + public var textAVAssetExportPreset960x540 = ypLocalized("960 x 540") + public var textAVAssetExportPresetPassthrough = ypLocalized("Original") } diff --git a/Source/Pages/Gallery/LibraryMediaManager.swift b/Source/Pages/Gallery/LibraryMediaManager.swift index 7b0d82bcb..9b10a56ea 100644 --- a/Source/Pages/Gallery/LibraryMediaManager.swift +++ b/Source/Pages/Gallery/LibraryMediaManager.swift @@ -9,70 +9,6 @@ import UIKit import Photos - -public enum compressionOptions: String { - case AVAssetExportPresetLowQuality - case AVAssetExportPreset640x480 - case AVAssetExportPresetMediumQuality - case AVAssetExportPreset1920x1080 - case AVAssetExportPreset1280x720 - case AVAssetExportPresetHighestQuality - case AVAssetExportPresetAppleM4A - case AVAssetExportPreset3840x2160 - case AVAssetExportPreset960x540 - case AVAssetExportPresetPassthrough - - func presetID() -> String { - switch self { - case .AVAssetExportPresetLowQuality: - return "AVAssetExportPresetLowQuality" - case .AVAssetExportPreset640x480: - return "AVAssetExportPreset640x480" - case .AVAssetExportPresetMediumQuality: - return "AVAssetExportPresetMediumQuality" - case .AVAssetExportPreset1920x1080: - return "AVAssetExportPreset1920x1080" - case .AVAssetExportPreset1280x720: - return "AVAssetExportPreset1280x720" - case .AVAssetExportPresetHighestQuality: - return "AVAssetExportPresetHighestQuality" - case .AVAssetExportPresetAppleM4A: - return "AVAssetExportPresetAppleM4A" - case .AVAssetExportPreset3840x2160: - return "AVAssetExportPreset3840x2160" - case .AVAssetExportPreset960x540: - return "AVAssetExportPreset960x540" - case .AVAssetExportPresetPassthrough: - return "AVAssetExportPresetPassthrough" - } - } - - func getLabel() -> String { - switch self { - case .AVAssetExportPresetLowQuality: - return "Low" - case .AVAssetExportPreset640x480: - return "HD" - case .AVAssetExportPresetMediumQuality: - return "Medium" - case .AVAssetExportPreset1920x1080: - return "Full HD" - case .AVAssetExportPreset1280x720: - return "1280 x 720" - case .AVAssetExportPresetHighestQuality: - return "Highest" - case .AVAssetExportPresetAppleM4A: - return "Apple M4A" - case .AVAssetExportPreset3840x2160: - return "3840 x 2160" - case .AVAssetExportPreset960x540: - return "960 x 540" - case .AVAssetExportPresetPassthrough: - return "Original" - } - } -} - class LibraryMediaManager { weak var v: YPLibraryView? diff --git a/Source/Pages/Gallery/YPLibraryVC.swift b/Source/Pages/Gallery/YPLibraryVC.swift index 344910b39..b28c389b5 100644 --- a/Source/Pages/Gallery/YPLibraryVC.swift +++ b/Source/Pages/Gallery/YPLibraryVC.swift @@ -261,7 +261,7 @@ internal final class YPLibraryVC: UIViewController, YPPermissionCheckable { } let options = PHFetchOptions() - options.sortDescriptors = [NSSortDescriptor(key: "modificationDate", ascending: false)] + options.sortDescriptors = [NSSortDescriptor(key: YPConfig.library.sortingOption.rawValue, ascending: false)] options.predicate = YPConfig.library.mediaType.predicate() return options } @@ -458,21 +458,20 @@ internal final class YPLibraryVC: UIViewController, YPPermissionCheckable { } if isVideoSelected { - let compressionOtions: [compressionOptions] = [compressionOptions.AVAssetExportPresetLowQuality, compressionOptions.AVAssetExportPreset640x480,compressionOptions.AVAssetExportPreset1920x1080, compressionOptions.AVAssetExportPresetPassthrough] - DispatchQueue.main.async { + let compressionOptions = YPImagePickerConfiguration.shared.video.compressionOption let storyBoard = UIStoryboard(name: "YPVideoCompressionVC", bundle: Bundle(for: YPVideoCompressionVC.self)) let ypVideoCompressionVC = storyBoard.instantiateViewController(withIdentifier: "YPVideoCompressionVC") as! YPVideoCompressionVC var titleArray = [String]() - for compressionOtion in compressionOtions { + for compressionOtion in compressionOptions { titleArray.append(compressionOtion.getLabel()) } ypVideoCompressionVC.titleArray = titleArray ypVideoCompressionVC.headingTitle = "Choose video quality" ypVideoCompressionVC.modalPresentationStyle = .overFullScreen ypVideoCompressionVC.didDismiss = {(index) in - YPImagePickerConfiguration.shared.video.compression = compressionOtions[index].presetID() + YPImagePickerConfiguration.shared.video.compression = compressionOptions[index].presetID() asyncCompressionGroup.leave() } self.present(ypVideoCompressionVC, animated: true, completion: nil)