Skip to content

eure/AssetsPicker

Repository files navigation

๐Ÿ“ธ MosaiqueAssetsPicker

Your customizable asset picker.

Overview

Whimsical AssetPicker

๐Ÿ”ถ Requirements

iOS 10.0+ Xcode 10.1+ Swift 4.2+

๐Ÿ“ฑ Features

  • Load camera rolls and others albums
  • Customize cells
  • Customize style and localization
  • Custom header
  • Photo on the cloud
  • Permissions handling
  • Other asset ( LivePhoto, Video, Gif, .. )
  • Background downloading
  • Selection counter ( badge ? )

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Usage

Default value

let photoPicker = MosaiqueAssetPickerViewController()
photoPicker.pickerDelegate = self

present(photoPicker, animated: true, completion: nil)

Customization

Use Custom Cell classes

let cellRegistrator = AssetPickerCellRegistrator()
cellRegistrator.register(cellClass: Demo2AssetCell.self, forCellType: .asset)
cellRegistrator.register(cellClass: Demo2AssetCollectionCell.self, forCellType: .assetCollection)

let photoPicker = MosaiqueAssetPickerViewController()
                    .setCellRegistrator(cellRegistrator)

photoPicker.pickerDelegate = self

present(photoPicker, animated: true, completion: nil)

Use Custom Nib classes

let assetNib = UINib(nibName: String(describing: Demo3AssetNib.self), bundle: nil)
let assetCollectionNib = UINib(nibName: String(describing: Demo3AssetCollectionNib.self), bundle: nil)

let cellRegistrator = AssetPickerCellRegistrator()
cellRegistrator.register(nib: assetNib, forCellType: .asset)
cellRegistrator.register(nib: assetCollectionNib, forCellType: .assetCollection)

let photoPicker = MosaiqueAssetPickerViewController()
                    .setCellRegistrator(cellRegistrator)

photoPicker.pickerDelegate = self

present(photoPicker, animated: true, completion: nil)

Add header view

let headerView = UIView()
headerView.backgroundColor = .orange
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.heightAnchor.constraint(equalToConstant: 120).isActive = true

let photoPicker = MosaiqueAssetPickerViewController()
                    .setHeaderView(headerView, isHeaderFloating: true)

photoPicker.pickerDelegate = self

present(photoPicker, animated: true, completion: nil)

Other customization

func setSelectionMode(_ selectionMode: SelectionMode)
func setSelectionMode(_ selectionColor: UIColor)
func setSelectionColor(_ tintColor: UIColor)
func setNumberOfItemsPerRow(_ numberOfItemsPerRow: Int)
func setHeaderView(_ headerView: UIView, isHeaderFloating: Bool)
func setCellRegistrator(_ cellRegistrator: AssetPickerCellRegistrator)
func setMediaTypes(_ supportOnlyMediaType: [PHAssetMediaType])
func disableOnLibraryScrollAnimation()
func localize(_ localize: LocalizedStrings)

public enum SelectionMode {
    case single
    case multiple(limit: Int)
}

public struct LocalizedStrings {
    public var done: String = "Done"
    public var next: String = "Next"
    public var dismiss: String = "Dismiss"
    public var collections: String = "Collections"
    public var changePermissions: String = "Change your Photo Library permissions"
}

AssetFuture usage

AssetFuture can be used to dismiss the view controller once the asset is selected but before the asset is ready/downloaded. It can be optained through the delegate:

func photoPicker(_ pickerController: UIViewController, didPickAssets assets: [AssetFuture])

You can retreive asynchronously a thumbnail with onThumbnailCompletion: ((Result<UIImage, NSError>) -> Void)? And the final image with finalImageResult: Result<UIImage, NSError>?

As long as the AssetFuture is retained by you or the MosaiqueAssetPickerViewController, the asset will be fetched, using the network if needed, even if the app enters background. The fetch request is cancelled on release.

iOS14

The new iOS version introduce lots of improvements on the traditional image picker. You can default to it if the user runs iOS 14 by using the new API:

   MosaiqueAssetPickerPresenter.controller(delegate: self)

Installation

CocoaPods

pod 'MosaiqueAssetsPicker'

SPM:

dependencies: [
    .package(url: "https://github.com/eure/AssetsPicker.git", .upToNextMajor(from: "1.3.0"))
]

What's using MosaiqueAssetsPicker

  • Pairs Engage

Contributing

Feel free to contribute. You can format your code using swiftformat:

swiftformat .

Install it with

brew install swiftformat

Authors

License

AssetsPicker is released under the MIT license.