UGCKitMediaPickerViewController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitMediaPickerViewController.h"
  3. #import <Photos/Photos.h>
  4. #import "UGCKitTheme.h"
  5. // ViewControllers
  6. #import "UGCKitAssetsViewController.h"
  7. #import "UGCKitAssetLoadingController.h"
  8. #import "UGCKitMediaPickerViewControllerPrivate.h"
  9. @interface _UGCMediaPickerNavigationController : UINavigationController
  10. @end
  11. @implementation UGCKitMediaPickerViewController
  12. - (instancetype)init {
  13. if (self = [self initWithConfig:nil theme:nil]) {
  14. }
  15. return self;
  16. }
  17. - (instancetype)initWithConfig:(UGCKitMediaPickerConfig *)config theme:(UGCKitTheme *)theme;
  18. {
  19. self = [super init];
  20. if (self) {
  21. _config = config ?: [[UGCKitMediaPickerConfig alloc] init];
  22. _theme = theme ?: [UGCKitTheme sharedTheme];
  23. self.showsNumberOfSelectedAssets = YES;
  24. self.mediaType = config.mediaType;
  25. // Set default values
  26. self.assetCollectionSubtypes = @[
  27. @(PHAssetCollectionSubtypeSmartAlbumUserLibrary),
  28. @(PHAssetCollectionSubtypeAlbumMyPhotoStream),
  29. @(PHAssetCollectionSubtypeSmartAlbumPanoramas),
  30. @(PHAssetCollectionSubtypeSmartAlbumVideos),
  31. @(PHAssetCollectionSubtypeSmartAlbumBursts)
  32. ];
  33. self.minimumNumberOfSelection = config.minItemCount;
  34. self.maximumNumberOfSelection = config.maxItemCount;
  35. self.allowsMultipleSelection = config.minItemCount != config.maxItemCount;
  36. _selectedAssets = [NSMutableOrderedSet orderedSet];
  37. // Get asset bundle
  38. if (theme.resourceBundle) {
  39. self.assetBundle = theme.resourceBundle;
  40. } else {
  41. self.assetBundle = [NSBundle bundleForClass:[self class]];
  42. NSString *bundlePath = [self.assetBundle pathForResource:@"UGCKitMediaPicker" ofType:@"bundle"];
  43. if (bundlePath) {
  44. self.assetBundle = [NSBundle bundleWithPath:bundlePath];
  45. }
  46. }
  47. [self setUpAlbumsViewController];
  48. self.view.backgroundColor = _theme.backgroundColor;
  49. // Set instance
  50. UGCKitAssetsViewController *albumsViewController = (UGCKitAssetsViewController *)self.childViewControllers.firstObject;
  51. albumsViewController.theme = _theme;
  52. albumsViewController.imagePickerController = self;
  53. }
  54. return self;
  55. }
  56. - (void)viewWillAppear:(BOOL)animated {
  57. [super viewWillAppear:animated];
  58. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  59. if (navigationBar) {
  60. UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), YES, 1);
  61. [_theme.backgroundColor set];
  62. UIRectFill(CGRectMake(0, 0, 1, 1));
  63. [navigationBar setBackgroundImage:UIGraphicsGetImageFromCurrentImageContext() forBarMetrics:UIBarMetricsDefault];
  64. UIGraphicsEndImageContext();
  65. navigationBar.barStyle = UIBarStyleBlack;
  66. [navigationBar setTranslucent:NO];
  67. navigationBar.shadowImage = [[UIImage alloc] init];
  68. [navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: _theme.titleColor}];
  69. }
  70. }
  71. - (UINavigationItem *)navigationItem
  72. {
  73. return self.assetViewController.navigationItem;
  74. }
  75. - (void)setUpAlbumsViewController
  76. {
  77. // Add UGCKitAlbumsViewController as a child
  78. NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"UGCKitResources" ofType:@"bundle"];
  79. NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; // bundle 不存在时会返回nil, 会从主bundle中找
  80. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"UGCKitMediaPicker" bundle:bundle];
  81. UGCKitAssetsViewController *assetViewController = [storyboard instantiateViewControllerWithIdentifier:@"UGCKitAssetsViewController"];
  82. assetViewController.theme = _theme;
  83. assetViewController.imagePickerController = self;
  84. [self addChildViewController:assetViewController];
  85. assetViewController.view.frame = self.view.bounds;
  86. [self.view addSubview:assetViewController.view];
  87. [assetViewController didMoveToParentViewController:self];
  88. [self.navigationController.navigationBar setItems:@[assetViewController.navigationItem]];
  89. self.assetViewController = assetViewController;
  90. }
  91. - (NSArray<AVAsset *> *)exportedAssets {
  92. return self.assetViewController.exportedAssets;
  93. }
  94. @end
  95. @implementation UGCKitMediaPickerConfig
  96. - (instancetype)init
  97. {
  98. self = [super init];
  99. if (self) {
  100. _minItemCount = 1;
  101. _maxItemCount = 1;
  102. _mediaType = UGCKitMediaTypeVideo;
  103. _numberOfColumnsInPortrait = 4;
  104. _numberOfColumnsInLandscape = 7;
  105. _combineVideos = YES;
  106. }
  107. return self;
  108. }
  109. @end
  110. @implementation _UGCMediaPickerNavigationController
  111. - (void)awakeFromNib {
  112. [super awakeFromNib];
  113. }
  114. - (UIStatusBarStyle)preferredStatusBarStyle
  115. {
  116. return UIStatusBarStyleLightContent;
  117. }
  118. - (UIViewController *)childForStatusBarStyle {
  119. return self.topViewController;
  120. }
  121. @end