CTAssetsPickerController.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. MIT License (MIT)
  3. Copyright (c) 2015 Clement CN Tsang
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #import <UIKit/UIKit.h>
  21. #import <Photos/Photos.h>
  22. NS_ASSUME_NONNULL_BEGIN
  23. @protocol CTAssetsPickerControllerDelegate;
  24. /**
  25. * A controller that allows picking multiple photos and videos from user's photo library.
  26. */
  27. @interface CTAssetsPickerController : UIViewController
  28. /**
  29. * The assets picker’s delegate object.
  30. */
  31. @property (nonatomic, weak) id <CTAssetsPickerControllerDelegate> delegate;
  32. /**
  33. * Set the `assetCollectionSubtypes` to specify which asset collections (albums) to be shown in the picker.
  34. *
  35. * You can specify which albums and their order to be shown in the picker by creating an `NSArray` of `NSNumber`
  36. * that containing the value of `PHAssetCollectionSubtype`.
  37. */
  38. @property (nonatomic, copy) NSArray<NSNumber*> *assetCollectionSubtypes;
  39. /**
  40. * Set the `defaultAssetCollection` to specify which asset collection (album) is the default asset collection.
  41. *
  42. * If the `defaultAssetCollection` is explictly set, the picker initially shows the content of default asset
  43. * collection instead of a list of albums. By default, there are no default asset collection.
  44. *
  45. * If there are more than one asset collections that match the subtype value of `defaultAssetCollection`, the
  46. * first matched asset collection will be the default asset collection.
  47. */
  48. @property (nonatomic, assign) PHAssetCollectionSubtype defaultAssetCollection;
  49. /**
  50. * Set the `PHFetchOptions` to specify options when fetching asset collections (albums).
  51. *
  52. * @see assetsFetchOptions
  53. */
  54. @property (nonatomic, strong) PHFetchOptions *assetCollectionFetchOptions;
  55. /**
  56. * Set the `PHFetchOptions` to specify options when fetching assets.
  57. *
  58. * @see assetCollectionFetchOptions
  59. */
  60. @property (nonatomic, strong) PHFetchOptions *assetsFetchOptions;
  61. /**
  62. * The selected assets.
  63. *
  64. * It contains selected `PHAsset` objects. The order of the objects is the selection order.
  65. *
  66. * You can use this property to select assets initially when presenting the picker.
  67. */
  68. @property (nonatomic, strong) NSMutableArray *selectedAssets;
  69. /**
  70. * An optional title for the done button
  71. *
  72. * You can override the title of "Done" button by this value.
  73. */
  74. @property (nonatomic, copy) NSString *doneButtonTitle;
  75. /**
  76. * Determines whether or not the cancel button is visible in the picker.
  77. *
  78. * The cancel button is visible by default. To hide the cancel button, (e.g. presenting the picker in `UIPopoverController`)
  79. * set this property’s value to `NO`.
  80. */
  81. @property (nonatomic, assign) BOOL showsCancelButton;
  82. /**
  83. * Determines whether or not the empty albums is shown in the album list.
  84. *
  85. * All albums are visible by default. To hide albums without assets matched with `assetsFetchOptions`,
  86. * set this property’s value to `NO`.
  87. *
  88. * @see assetsFetchOptions
  89. */
  90. @property (nonatomic, assign) BOOL showsEmptyAlbums;
  91. /**
  92. * Determines whether or not the number of assets is shown in the album list.
  93. *
  94. * The number of assets is visible by default. To hide the number of assets, (e.g. implementing `shouldShowAsset` delegate method)
  95. * set this property’s value to `NO`.
  96. */
  97. @property (nonatomic, assign) BOOL showsNumberOfAssets;
  98. /**
  99. * Determines whether or not the done button is always enabled.
  100. *
  101. * The done button is enabled only when assets are selected. To enable the done button even without assets selected,
  102. * set this property’s value to `YES`.
  103. */
  104. @property (nonatomic, assign) BOOL alwaysEnableDoneButton;
  105. /**
  106. * Determines whether or not the selection order is shown in the grid view.
  107. *
  108. * Only a checkmark is shown on selected assets by default. To shows the order of selection,
  109. * set this property’s value to `YES`.
  110. */
  111. @property (nonatomic, assign) BOOL showsSelectionIndex;
  112. /**
  113. * The split view controller of the picker hierarchy. (read-only)
  114. *
  115. * This property contains the child split view controller of the picker.
  116. */
  117. @property (nonatomic, readonly, strong) UISplitViewController *childSplitViewController;
  118. /**
  119. * @name Managing Selections
  120. */
  121. /**
  122. * Selects an asset in the picker.
  123. *
  124. * @param asset The asset to be selected.
  125. *
  126. * @see deselectAsset:
  127. */
  128. - (void)selectAsset:(PHAsset *)asset;
  129. /**
  130. * Deselects an asset in the picker.
  131. *
  132. * @param asset The asset to be deselected.
  133. *
  134. * @see selectAsset:
  135. */
  136. - (void)deselectAsset:(PHAsset *)asset;
  137. @end
  138. /**
  139. * The `CTAssetsPickerControllerDelegate` protocol defines methods that allow you to to interact with the assets picker interface
  140. * and manage the selection and highlighting of assets in the picker.
  141. *
  142. * The methods of this protocol notify your delegate when the user selects, highlights, finish picking assets, or cancels the picker operation.
  143. *
  144. * The delegate methods are responsible for dismissing the picker when the operation completes.
  145. * To dismiss the picker, call the `dismissViewControllerAnimated:completion:` method of the presenting controller
  146. * responsible for displaying `CTAssetsPickerController` object.
  147. *
  148. * The picked assets are `PHAsset` objects and contain only metadata. The underlying image or video data for any given asset might not be stored on the local device.
  149. * You have to use `PHImageManager` object for loading image or video data associated with a `PHAsset`.
  150. */
  151. @protocol CTAssetsPickerControllerDelegate <NSObject>
  152. /**
  153. * @name Closing the Picker
  154. */
  155. /**
  156. * Tells the delegate that the user finish picking photos or videos.
  157. *
  158. * @param picker The controller object managing the assets picker interface.
  159. * @param assets An array containing picked `PHAsset` objects.
  160. *
  161. * @see assetsPickerControllerDidCancel:
  162. */
  163. - (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray<PHAsset*> *)assets;
  164. @optional
  165. /**
  166. * Tells the delegate that the user cancelled the pick operation.
  167. *
  168. * @param picker The controller object managing the assets picker interface.
  169. *
  170. * @see assetsPickerController:didFinishPickingAssets:
  171. */
  172. - (void)assetsPickerControllerDidCancel:(CTAssetsPickerController *)picker;
  173. /**
  174. * @name Configuting Asset Selection View
  175. */
  176. /**
  177. * Ask the delegate the layout of asset selection view (UICollectionView).
  178. *
  179. * @param picker The controller object managing the assets picker interface.
  180. * @param contentSize The bounds size of current view.
  181. * @param trait The trait collection of current view.
  182. *
  183. * @return Custom `UICollectionViewLayout` for the asset selection view.
  184. */
  185. - (UICollectionViewLayout *)assetsPickerController:(CTAssetsPickerController *)picker collectionViewLayoutForContentSize:(CGSize)contentSize traitCollection:(UITraitCollection *)trait;
  186. /**
  187. * Ask the delegate if the asset selection view should sroll to bottom on shown.
  188. *
  189. * @param picker The controller object managing the assets picker interface.
  190. * @param assetCollection The asset collection of asset selection view.
  191. *
  192. * @return `YES` (the default) if the asset grid should scroll to bottom on shown or `NO` if it should not.
  193. */
  194. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldScrollToBottomForAssetCollection:(PHAssetCollection *)assetCollection;
  195. /**
  196. * @name Enabling Assets
  197. */
  198. /**
  199. * Ask the delegate if the specified asset should be enabled for selection.
  200. *
  201. * @param picker The controller object managing the assets picker interface.
  202. * @param asset The asset to be enabled.
  203. *
  204. * @return `YES` if the asset should be enabled or `NO` if it should not.
  205. *
  206. * @see assetsPickerController:shouldShowAsset:
  207. */
  208. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldEnableAsset:(PHAsset *)asset;
  209. /**
  210. * @name Managing the Selected Assets
  211. */
  212. /**
  213. * Asks the delegate if the specified asset should be selected.
  214. *
  215. * @param picker The controller object managing the assets picker interface.
  216. * @param asset The asset to be selected.
  217. *
  218. * @return `YES` if the asset should be selected or `NO` if it should not.
  219. *
  220. * @see assetsPickerController:shouldDeselectAsset:
  221. */
  222. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset;
  223. /**
  224. * Tells the delegate that the asset was selected.
  225. *
  226. * @param picker The controller object managing the assets picker interface.
  227. * @param asset The asset that was selected.
  228. *
  229. * @see assetsPickerController:didDeselectAsset:
  230. */
  231. - (void)assetsPickerController:(CTAssetsPickerController *)picker didSelectAsset:(PHAsset *)asset;
  232. /**
  233. * Asks the delegate if the specified asset should be deselected.
  234. *
  235. * @param picker The controller object managing the assets picker interface.
  236. * @param asset The asset to be deselected.
  237. *
  238. * @return `YES` if the asset should be deselected or `NO` if it should not.
  239. *
  240. * @see assetsPickerController:shouldSelectAsset:
  241. */
  242. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldDeselectAsset:(PHAsset *)asset;
  243. /**
  244. * Tells the delegate that the item at the specified path was deselected.
  245. *
  246. * @param picker The controller object managing the assets picker interface.
  247. * @param asset The asset that was deselected.
  248. *
  249. * @see assetsPickerController:didSelectAsset:
  250. */
  251. - (void)assetsPickerController:(CTAssetsPickerController *)picker didDeselectAsset:(PHAsset *)asset;
  252. /**
  253. * @name Managing Asset Highlighting
  254. */
  255. /**
  256. * Asks the delegate if the specified asset should be highlighted.
  257. *
  258. * @param picker The controller object managing the assets picker interface.
  259. * @param asset The asset to be highlighted.
  260. *
  261. * @return `YES` if the asset should be highlighted or `NO` if it should not.
  262. */
  263. - (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldHighlightAsset:(PHAsset *)asset;
  264. /**
  265. * Tells the delegate that asset was highlighted.
  266. *
  267. * @param picker The controller object managing the assets picker interface.
  268. * @param asset The asset that was highlighted.
  269. *
  270. * @see assetsPickerController:didUnhighlightAsset:
  271. */
  272. - (void)assetsPickerController:(CTAssetsPickerController *)picker didHighlightAsset:(PHAsset *)asset;
  273. /**
  274. * Tells the delegate that the highlight was removed from the asset.
  275. *
  276. * @param picker The controller object managing the assets picker interface.
  277. * @param asset The asset that had its highlight removed.
  278. *
  279. * @see assetsPickerController:didHighlightAsset:
  280. */
  281. - (void)assetsPickerController:(CTAssetsPickerController *)picker didUnhighlightAsset:(PHAsset *)asset;
  282. /**
  283. * @name Notifications
  284. */
  285. /**
  286. * Sent when the assets selected or deselected
  287. *
  288. * The notification’s `object` is an `NSArray` object of selected assets
  289. */
  290. extern NSString * const CTAssetsPickerSelectedAssetsDidChangeNotification;
  291. /**
  292. * Sent when asset is selected
  293. *
  294. * The notification’s `object` is a `PHAsset` that is selected
  295. */
  296. extern NSString * const CTAssetsPickerDidSelectAssetNotification;
  297. /**
  298. * Sent when asset is deselected
  299. *
  300. * The notification’s `object` is a `PHAsset` that is deselected
  301. */
  302. extern NSString * const CTAssetsPickerDidDeselectAssetNotification;
  303. @end
  304. NS_ASSUME_NONNULL_END