Skip to content

Commit

Permalink
Merge pull request #9 from niralishaha25/dev
Browse files Browse the repository at this point in the history
The compression and sorting option is completely configurable
  • Loading branch information
niralishaha25 authored Nov 10, 2022
2 parents ac4c5ff + a669169 commit 020dc3d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 69 deletions.
3 changes: 3 additions & 0 deletions Example/ExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down
74 changes: 74 additions & 0 deletions Source/Configuration/YPImagePickerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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
}
}
}
11 changes: 11 additions & 0 deletions Source/Configuration/YPWordings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
64 changes: 0 additions & 64 deletions Source/Pages/Gallery/LibraryMediaManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
9 changes: 4 additions & 5 deletions Source/Pages/Gallery/YPLibraryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 020dc3d

Please sign in to comment.