QBImagePickerController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. Copyright (c) 2013 Katsuma Tanaka
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  6. */
  7. #import "QBImagePickerController.h"
  8. // Views
  9. #import "QBImagePickerGroupCell.h"
  10. // Controllers
  11. #import "QBAssetCollectionViewController.h"
  12. @interface QBImagePickerController ()
  13. @property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
  14. @property (nonatomic, strong) NSMutableArray *assetsGroups;
  15. @property (nonatomic, strong) UITableView *tableView;
  16. @property (nonatomic, assign) UIBarStyle previousBarStyle;
  17. @property (nonatomic, assign) BOOL previousBarTranslucent;
  18. @property (nonatomic, assign) UIStatusBarStyle previousStatusBarStyle;
  19. @property (nonatomic, assign) UIView *navigationBarView;
  20. - (void)cancel;
  21. - (NSDictionary *)mediaInfoFromAsset:(ALAsset *)asset;
  22. @end
  23. @implementation QBImagePickerController
  24. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  25. {
  26. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  27. if(self) {
  28. /* Check sources */
  29. [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
  30. [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
  31. [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
  32. /* Initialization */
  33. self.title = @"Photos";
  34. self.filterType = QBImagePickerFilterTypeAllPhotos;
  35. self.showsCancelButton = YES;
  36. self.fullScreenLayoutEnabled = YES;
  37. self.allowsMultipleSelection = NO;
  38. self.limitsMinimumNumberOfSelection = NO;
  39. self.limitsMaximumNumberOfSelection = NO;
  40. self.minimumNumberOfSelection = 0;
  41. self.maximumNumberOfSelection = 0;
  42. ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
  43. self.assetsLibrary = assetsLibrary;
  44. // [assetsLibrary release];
  45. self.assetsGroups = [NSMutableArray array];
  46. // Table View
  47. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT - JX_SCREEN_TOP) style:UITableViewStylePlain];
  48. tableView.dataSource = self;
  49. tableView.delegate = self;
  50. tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  51. [self.view addSubview:tableView];
  52. self.tableView = tableView;
  53. self.tableView.estimatedRowHeight = 0;
  54. self.tableView.estimatedSectionFooterHeight = 0;
  55. self.tableView.estimatedSectionHeaderHeight = 0;
  56. // [tableView release];
  57. }
  58. return self;
  59. }
  60. - (void)viewDidLoad
  61. {
  62. [super viewDidLoad];
  63. void (^assetsGroupsEnumerationBlock)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *assetsGroup, BOOL *stop) {
  64. if(assetsGroup) {
  65. switch(self.filterType) {
  66. case QBImagePickerFilterTypeAllAssets:
  67. [assetsGroup setAssetsFilter:[ALAssetsFilter allAssets]];
  68. break;
  69. case QBImagePickerFilterTypeAllPhotos:
  70. [assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
  71. break;
  72. case QBImagePickerFilterTypeAllVideos:
  73. [assetsGroup setAssetsFilter:[ALAssetsFilter allVideos]];
  74. break;
  75. }
  76. if(assetsGroup.numberOfAssets > 0) {
  77. [self.assetsGroups addObject:assetsGroup];
  78. [self.tableView reloadData];
  79. }
  80. }
  81. };
  82. void (^assetsGroupsFailureBlock)(NSError *) = ^(NSError *error) {
  83. NSLog(@"Error: %@", [error localizedDescription]);
  84. };
  85. // Enumerate Camera Roll
  86. [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  87. // Photo Stream
  88. [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupPhotoStream usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  89. // Album
  90. [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  91. // Event
  92. [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupEvent usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  93. // Faces
  94. [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupFaces usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
  95. }
  96. - (void)viewWillAppear:(BOOL)animated
  97. {
  98. [super viewWillAppear:animated];
  99. self.navigationController.navigationBar.hidden = YES;
  100. [self createNavgationBar];
  101. self.showsCancelButton = YES;
  102. // Full screen layout
  103. if(self.fullScreenLayoutEnabled) {
  104. NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
  105. if(indexPath == nil) {
  106. self.previousBarStyle = self.navigationController.navigationBar.barStyle;
  107. self.previousBarTranslucent = self.navigationController.navigationBar.translucent;
  108. self.previousStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
  109. self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
  110. self.navigationController.navigationBar.translucent = YES;
  111. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
  112. // CGFloat top = 0;
  113. // if(![[UIApplication sharedApplication] isStatusBarHidden]) top = top + 20;
  114. // if(!self.navigationController.navigationBarHidden) top = top + 44;
  115. // self.tableView.contentInset = UIEdgeInsetsMake(top, 0, 0, 0);
  116. // self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(top, 0, 0, 0);
  117. //
  118. // [self setWantsFullScreenLayout:YES];
  119. }
  120. }
  121. // Cancel table view selection
  122. [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
  123. }
  124. - (void) createNavgationBar {
  125. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_TOP)];
  126. if (THESIMPLESTYLE) {
  127. view.backgroundColor = [UIColor whiteColor];
  128. }else {
  129. view.backgroundColor = THEMECOLOR;
  130. }
  131. [self.view addSubview:view];
  132. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP - 30, 200, 20)];
  133. label.font = [UIFont systemFontOfSize:17];
  134. label.textColor = [UIColor whiteColor];
  135. label.textAlignment = NSTextAlignmentCenter;
  136. label.text = @"photos";
  137. label.center = CGPointMake(view.frame.size.width / 2, label.center.y);
  138. [view addSubview:label];
  139. self.navigationBarView = view;
  140. }
  141. - (void)viewDidAppear:(BOOL)animated
  142. {
  143. [super viewDidAppear:animated];
  144. // Flash scroll indicators
  145. [self.tableView flashScrollIndicators];
  146. }
  147. - (void)viewWillDisappear:(BOOL)animated
  148. {
  149. [super viewWillDisappear:animated];
  150. // Restore bar styles
  151. self.navigationController.navigationBar.barStyle = self.previousBarStyle;
  152. self.navigationController.navigationBar.translucent = self.previousBarTranslucent;
  153. [[UIApplication sharedApplication] setStatusBarStyle:self.previousStatusBarStyle animated:YES];
  154. }
  155. - (void)setShowsCancelButton:(BOOL)showsCancelButton
  156. {
  157. _showsCancelButton = showsCancelButton;
  158. if(self.showsCancelButton) {
  159. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH -90, JX_SCREEN_TOP - 34, 80, 25)];
  160. [btn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
  161. btn.titleLabel.font = [UIFont systemFontOfSize:17];
  162. [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  163. [btn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
  164. [self.navigationBarView addSubview:btn];
  165. // UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
  166. // [self.navigationItem setRightBarButtonItem:cancelButton animated:NO];
  167. // [cancelButton release];
  168. } else {
  169. [self.navigationItem setRightBarButtonItem:nil animated:NO];
  170. }
  171. }
  172. - (void)dealloc
  173. {
  174. // [_assetsLibrary release];
  175. // [_assetsGroups release];
  176. //
  177. // [_tableView release];
  178. //
  179. // [super dealloc];
  180. }
  181. #pragma mark - Instance Methods
  182. - (void)cancel
  183. {
  184. if([self.delegate respondsToSelector:@selector(imagePickerControllerDidCancel:)]) {
  185. [self.delegate imagePickerControllerDidCancel:self];
  186. }
  187. }
  188. - (NSDictionary *)mediaInfoFromAsset:(ALAsset *)asset
  189. {
  190. NSMutableDictionary *mediaInfo = [NSMutableDictionary dictionary];
  191. [mediaInfo setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
  192. [mediaInfo setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
  193. [mediaInfo setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];
  194. return mediaInfo;
  195. }
  196. #pragma mark - UITableViewDataSource
  197. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  198. {
  199. return 1;
  200. }
  201. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  202. {
  203. return self.assetsGroups.count;
  204. }
  205. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  206. {
  207. static NSString *cellIdentifier = @"Cell";
  208. QBImagePickerGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  209. if(cell == nil) {
  210. cell = [[QBImagePickerGroupCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  211. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  212. }
  213. ALAssetsGroup *assetsGroup = [self.assetsGroups objectAtIndex:indexPath.row];
  214. cell.imageView.image = [UIImage imageWithCGImage:assetsGroup.posterImage];
  215. cell.titleLabel.text = [NSString stringWithFormat:@"%@", [assetsGroup valueForProperty:ALAssetsGroupPropertyName]];
  216. cell.countLabel.text = [NSString stringWithFormat:@"(%ld)", assetsGroup.numberOfAssets];
  217. return cell;
  218. }
  219. #pragma mark - UITableViewDelegate
  220. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  221. {
  222. return 60;
  223. }
  224. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  225. {
  226. ALAssetsGroup *assetsGroup = [self.assetsGroups objectAtIndex:indexPath.row];
  227. BOOL showsHeaderButton = ([self.delegate respondsToSelector:@selector(descriptionForSelectingAllAssets:)] && [self.delegate respondsToSelector:@selector(descriptionForDeselectingAllAssets:)]);
  228. BOOL showsFooterDescription = NO;
  229. switch(self.filterType) {
  230. case QBImagePickerFilterTypeAllAssets:
  231. showsFooterDescription = ([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:numberOfVideos:)]);
  232. break;
  233. case QBImagePickerFilterTypeAllPhotos:
  234. showsFooterDescription = ([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:)]);
  235. break;
  236. case QBImagePickerFilterTypeAllVideos:
  237. showsFooterDescription = ([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfVideos:)]);
  238. break;
  239. }
  240. // Show assets collection view
  241. QBAssetCollectionViewController *assetCollectionViewController = [[QBAssetCollectionViewController alloc] init];
  242. assetCollectionViewController.title = [assetsGroup valueForProperty:ALAssetsGroupPropertyName];
  243. assetCollectionViewController.delegate = self;
  244. assetCollectionViewController.assetsGroup = assetsGroup;
  245. assetCollectionViewController.filterType = self.filterType;
  246. assetCollectionViewController.showsCancelButton = self.showsCancelButton;
  247. assetCollectionViewController.fullScreenLayoutEnabled = self.fullScreenLayoutEnabled;
  248. assetCollectionViewController.showsHeaderButton = showsHeaderButton;
  249. assetCollectionViewController.showsFooterDescription = showsFooterDescription;
  250. assetCollectionViewController.allowsMultipleSelection = self.allowsMultipleSelection;
  251. assetCollectionViewController.limitsMinimumNumberOfSelection = self.limitsMinimumNumberOfSelection;
  252. assetCollectionViewController.limitsMaximumNumberOfSelection = self.limitsMaximumNumberOfSelection;
  253. assetCollectionViewController.minimumNumberOfSelection = self.minimumNumberOfSelection;
  254. assetCollectionViewController.maximumNumberOfSelection = self.maximumNumberOfSelection;
  255. [self.navigationController pushViewController:assetCollectionViewController animated:YES];
  256. // [assetCollectionViewController release];
  257. }
  258. #pragma mark - QBAssetCollectionViewControllerDelegate
  259. - (void)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController didFinishPickingAsset:(ALAsset *)asset
  260. {
  261. if([self.delegate respondsToSelector:@selector(imagePickerControllerWillFinishPickingMedia:)]) {
  262. [self.delegate imagePickerControllerWillFinishPickingMedia:self];
  263. }
  264. if([self.delegate respondsToSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)]) {
  265. [self.delegate imagePickerController:self didFinishPickingMediaWithInfo:[self mediaInfoFromAsset:asset]];
  266. }
  267. }
  268. - (void)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController didFinishPickingAssets:(NSArray *)assets
  269. {
  270. if([self.delegate respondsToSelector:@selector(imagePickerControllerWillFinishPickingMedia:)]) {
  271. [self.delegate imagePickerControllerWillFinishPickingMedia:self];
  272. }
  273. if([self.delegate respondsToSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)]) {
  274. NSMutableArray *info = [NSMutableArray array];
  275. for(ALAsset *asset in assets) {
  276. [info addObject:[self mediaInfoFromAsset:asset]];
  277. }
  278. [self.delegate imagePickerController:self didFinishPickingMediaWithInfo:info];
  279. }
  280. }
  281. - (void)assetCollectionViewControllerDidCancel:(QBAssetCollectionViewController *)assetCollectionViewController
  282. {
  283. if([self.delegate respondsToSelector:@selector(imagePickerControllerDidCancel:)]) {
  284. [self.delegate imagePickerControllerDidCancel:self];
  285. }
  286. }
  287. - (NSString *)descriptionForSelectingAllAssets:(QBAssetCollectionViewController *)assetCollectionViewController
  288. {
  289. NSString *description = nil;
  290. if([self.delegate respondsToSelector:@selector(descriptionForSelectingAllAssets:)]) {
  291. description = [self.delegate descriptionForSelectingAllAssets:self];
  292. }
  293. return description;
  294. }
  295. - (NSString *)descriptionForDeselectingAllAssets:(QBAssetCollectionViewController *)assetCollectionViewController
  296. {
  297. NSString *description = nil;
  298. if([self.delegate respondsToSelector:@selector(descriptionForDeselectingAllAssets:)]) {
  299. description = [self.delegate descriptionForDeselectingAllAssets:self];
  300. }
  301. return description;
  302. }
  303. - (NSString *)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController descriptionForNumberOfPhotos:(NSUInteger)numberOfPhotos
  304. {
  305. NSString *description = nil;
  306. if([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:)]) {
  307. description = [self.delegate imagePickerController:self descriptionForNumberOfPhotos:numberOfPhotos];
  308. }
  309. return description;
  310. }
  311. - (NSString *)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController descriptionForNumberOfVideos:(NSUInteger)numberOfVideos
  312. {
  313. NSString *description = nil;
  314. if([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfVideos:)]) {
  315. description = [self.delegate imagePickerController:self descriptionForNumberOfVideos:numberOfVideos];
  316. }
  317. return description;
  318. }
  319. - (NSString *)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController descriptionForNumberOfPhotos:(NSUInteger)numberOfPhotos numberOfVideos:(NSUInteger)numberOfVideos
  320. {
  321. NSString *description = nil;
  322. if([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:numberOfVideos:)]) {
  323. description = [self.delegate imagePickerController:self descriptionForNumberOfPhotos:numberOfPhotos numberOfVideos:numberOfVideos];
  324. }
  325. return description;
  326. }
  327. @end