HXPHPicker
repository·main·Indexed 18 days ago
https://github.com/silencelove/hxphpickerA comprehensive photo and video selector for iOS and iPadOS. HXPHPicker supports multiple media types including Photos, GIFs, Live Photos, and Videos, and features iCloud asset downloading and built-in photo/video editing capabilities. It is available in several modular versions (Full, Lite, Picker, Editor, and Camera) to optimize binary size, with support for iOS 12.0+ (or iOS 10.0+ via HXPHPicker-Lite).
What's inside HXPHPicker
- HXPHPicker is a Swift-based toolkit designed for selecting, editing, and displaying images. It provides a comprehensive suite of tools for handling image-related workflows in iOS applications, including a picker for selecting media, an editor for modifications, and components for displaying images.
Handle iCloud Synchronization behavior
mainThe
allowSyncICloudWhenSelectPhotosetting determines how the picker handles assets stored in iCloud:- If
true: The picker will attempt to sync iCloud resources before selection. If the network is down or iCloud fails, the selection will fail. - If
false: The picker will not attempt sync. If the resource is only in iCloud, fetching the original image will fail.
- If
Manage local camera assets across sessions
mainThe picker supports persisting locally captured camera assets. When the picker is dismissed, the
pickerController(_:didDismissComplete:)method provides an array of[PhotoAsset]representing local camera data. You can store these and re-inject them into the next picker instance by setting thelocalCameraAssetArrayproperty on thePhotoPickerController.// 1. Capture local assets on dismiss func pickerController(_ pickerController: PhotoPickerController, didDismissComplete localCameraAssetArray: [PhotoAsset]) { self.savedLocalAssets = localCameraAssetArray } // 2. Re-inject assets on next launch let config = PickerConfiguration() let pickerController = PhotoPickerController(picker: config, delegate: self) pickerController.localCameraAssetArray = self.savedLocalAssets present(pickerController, animated: true, completion: nil)Install HXPHPicker via CocoaPods
mainTo install using CocoaPods, add the following line to your
Podfile:pod 'HXPHPicker'Then run the installation command in your terminal:
$ pod installConfigure Info.plist permissions
mainTo use the Picker, Camera, or Library features, you must add the following keys to your
Info.plistfile depending on the module you are using:| Key | Module | Info | | ----- | ---- | ---- | | NSPhotoLibraryUsageDescription | Picker | Allow access to album | | NSPhotoLibraryAddUsageDescription | Picker | Allow to save pictures to album | | PHPhotoLibraryPreventAutomaticLimitedAccessAlert | Picker | Set YES to prevent automatic limited access alert in iOS 14+ | | NSCameraUsageDescription | Camera | Allow camera | | NSMicrophoneUsageDescription | Camera | Allow microphone |Quick Start: Present the Photo Picker
mainYou can present the photo picker using two different methods.
Method 1: Using
PhotoPickerControllerdirectly This method gives you more control via a delegate. You can set thepickerDelegate, pre-select assets usingselectedAssetArray, and toggle original image selection withisOriginal.Method 2: Using the
Photo.pickerconvenience method This is a closure-based approach that provides aresult(containing selected assets and original status) on success and acancelcallback on cancellation.import HXPHPicker class ViewController: UIViewController, PhotoPickerControllerDelegate { func presentPickerController() { let config = PickerConfiguration.default // Method 1: Direct Controller let pickerController = PhotoPickerController(picker: config) pickerController.pickerDelegate = self pickerController.selectedAssetArray = selectedAssets pickerController.isOriginal = isOriginal present(pickerController, animated: true, completion: nil) // Method 2: Convenience Method Photo.picker(config) { result, pickerController in // result.photoAssets: Currently selected data // result.isOriginal: Whether the original image is selected } cancel: { pickerController in // Cancelled callback } } // Delegate methods func pickerController(_ pickerController: PhotoPickerController, didFinishSelection result: PickerResult) { result.getImage { (image, photoAsset, index) in if let image = image { print("success", image) } } completionHandler: { (images) in print(images) } } func pickerController(didCancel pickerController: PhotoPickerController) { // Handle cancel } }Use the Photo Picker via PhotoPickerControllerDelegate
mainFor more control, you can initialize a
PhotoPickerControllerand conform to thePhotoPickerControllerDelegate.Important Note on Dismissal: By default, the controller dismisses itself automatically upon completion or cancellation. If you need to manage the dismissal manually, set the
autoDismissproperty of the picker tofalse.To retrieve the actual images from the
PickerResult, use thegetImagemethod which provides both an individual asset callback and a final completion handler.// 1. Initialize and present let config = PickerConfiguration() let pickerController = PhotoPickerController(picker: config) pickerController.pickerDelegate = self present(pickerController, animated: true, completion: nil) // 2. Implement Delegate methods func pickerController(_ pickerController: PhotoPickerController, didFinishSelection result: PickerResult) { // Retrieve images from assets result.getImage { (image, photoAsset, index) in if let image = image { print("success", image) } else { print("failed") } } completionHandler: { (images) in // All images have been processed } } func pickerController(didCancel pickerController: PhotoPickerController) { // Handle cancellation }Initialize the Video Editor
mainTo use the video editor, first initialize a
VideoEditorConfigurationobject. You can then instantiate anEditorControllerusing one of the following methods: a localURL, anAVAsset, or a network URL.// Initialize configuration first let config = VideoEditorConfiguration() // Option 1: Initialize from a local video URL let controller = EditorController(videoURL: videoURL, config: config, delegate: self) // Option 2: Initialize from an AVAsset let controller = EditorController(avAsset: avAsset, config: config, delegate: self) // Option 3: Initialize from a network video URL let controller = EditorController(networkVideoURL: url, config: config, delegate: self) present(controller, animated: true)Preview assets using PhotoPickerController
mainTo show a preview of selected assets using a controller, use
PhotoPickerControllerwith a configuration object.- Get a configuration using
PhotoTools.getWXPickerConfig(). - Initialize
PhotoPickerControllerwith the config, a starting index, and a delegate. - Assign the array of assets to
selectedAssetArray. - Present the controller.
let previewConfig = PhotoTools.getWXPickerConfig() let previewController = PhotoPickerController(preview: previewConfig, currentIndex: 0, delegate: self) previewController.selectedAssetArray = selectedAssets present(previewController, animated: true, completion: nil)- Get a configuration using
Preview assets using PhotoBrowser
mainFor a more interactive preview (including deletion and long-press support), use
PhotoBrowser.show.Key parameters:
selectedAssets: The array of assets to preview.pageIndex: The starting position.config: APhotoBrowser.Configurationobject to control UI behavior (e.g.,showDelete).transitionalImage: An initialUIImagefor the transition animation.deleteAssetHandler: A callback triggered when the delete button is pressed. UsephotoBrowser.deleteCurrentPreviewPhotoAsset()inside this handler to perform the deletion.longPressHandler: A callback for long-press events on assets.
let config = PhotoBrowser.Configuration() config.showDelete = true PhotoBrowser.show( selectedAssets, pageIndex: indexPath.item, config: config, transitionalImage: cell?.imageView.image ) { index in // Transition handler } deleteAssetHandler: { index, photoAsset, photoBrowser in // Handle deletion photoBrowser.deleteCurrentPreviewPhotoAsset() } longPressHandler: { index, photoAsset, photoBrowser in // Handle long press }Install HXPHPicker via Swift Package Manager
mainAdd HXPHPicker as a dependency in your
Package.swiftfile.⚠️ Requirement: You must use Xcode 12.0 or higher to support the addition of resource files and localization files.
dependencies: [ .package(url: "https://github.com/SilenceLove/HXPHPicker.git", .upToNextMajor(from: "2.0")) ]Install Kingfisher via SPM, CocoaPods, or Carthage
mainYou can integrate Kingfisher into your project using several dependency managers:
Swift Package Manager
- Go to File > Swift Packages > Add Package Dependency.
- Add the URL:
https://github.com/onevcat/Kingfisher.git. - Select Up to Next Major version starting from
7.0.0.
CocoaPods
Add the following to your
Podfile:source 'https://github.com/CocoaPods/Specs.git' platform :ios, '12.0' use_frameworks! target 'MyApp' do pod 'Kingfisher', '~> 7.0' endCarthage
Add the following to your
Cartfile:github "onevcat/Kingfisher" ~> 7.0pod 'Kingfisher', '~> 7.0'