猎豆优选

QBAlbumsViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // QBAlbumsViewController.m
  3. // QBImagePicker
  4. //
  5. // Created by Katsuma Tanaka on 2015/04/03.
  6. // Copyright (c) 2015 Katsuma Tanaka. All rights reserved.
  7. //
  8. #import "QBAlbumsViewController.h"
  9. #import <Photos/Photos.h>
  10. // Views
  11. #import "QBAlbumCell.h"
  12. // ViewControllers
  13. #import "QBImagePickerController.h"
  14. #import "QBAssetsViewController.h"
  15. static CGSize CGSizeScale(CGSize size, CGFloat scale) {
  16. return CGSizeMake(size.width * scale, size.height * scale);
  17. }
  18. @interface QBImagePickerController (Private)
  19. @property (nonatomic, strong) NSBundle *assetBundle;
  20. @end
  21. @interface QBAlbumsViewController () <PHPhotoLibraryChangeObserver>
  22. @property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton;
  23. @property (nonatomic, copy) NSArray *fetchResults;
  24. @property (nonatomic, copy) NSArray *assetCollections;
  25. @end
  26. @implementation QBAlbumsViewController
  27. - (void)viewDidLoad
  28. {
  29. [super viewDidLoad];
  30. [self setUpToolbarItems];
  31. // Fetch user albums and smart albums
  32. PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
  33. PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
  34. self.fetchResults = @[smartAlbums, userAlbums];
  35. [self updateAssetCollections];
  36. // Register observer
  37. [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated
  40. {
  41. [super viewWillAppear:animated];
  42. // Configure navigation item
  43. self.navigationItem.title = NSLocalizedStringFromTableInBundle(@"albums.title", @"QBImagePicker", self.imagePickerController.assetBundle, nil);
  44. self.navigationItem.prompt = self.imagePickerController.prompt;
  45. // Show/hide 'Done' button
  46. if (self.imagePickerController.allowsMultipleSelection) {
  47. [self.navigationItem setRightBarButtonItem:self.doneButton animated:NO];
  48. } else {
  49. [self.navigationItem setRightBarButtonItem:nil animated:NO];
  50. }
  51. [self updateControlState];
  52. [self updateSelectionInfo];
  53. }
  54. - (void)dealloc
  55. {
  56. // Deregister observer
  57. [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
  58. }
  59. #pragma mark - Storyboard
  60. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  61. {
  62. QBAssetsViewController *assetsViewController = segue.destinationViewController;
  63. assetsViewController.imagePickerController = self.imagePickerController;
  64. assetsViewController.assetCollection = self.assetCollections[self.tableView.indexPathForSelectedRow.row];
  65. }
  66. #pragma mark - Actions
  67. - (IBAction)cancel:(id)sender
  68. {
  69. if ([self.imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerControllerDidCancel:)]) {
  70. [self.imagePickerController.delegate qb_imagePickerControllerDidCancel:self.imagePickerController];
  71. }
  72. }
  73. - (IBAction)done:(id)sender
  74. {
  75. if ([self.imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didFinishPickingAssets:)]) {
  76. [self.imagePickerController.delegate qb_imagePickerController:self.imagePickerController
  77. didFinishPickingAssets:self.imagePickerController.selectedAssets.array];
  78. }
  79. }
  80. #pragma mark - Toolbar
  81. - (void)setUpToolbarItems
  82. {
  83. // Space
  84. UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
  85. UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
  86. // Info label
  87. NSDictionary *attributes = @{ NSForegroundColorAttributeName: [UIColor blackColor] };
  88. UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:NULL];
  89. infoButtonItem.enabled = NO;
  90. [infoButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
  91. [infoButtonItem setTitleTextAttributes:attributes forState:UIControlStateDisabled];
  92. self.toolbarItems = @[leftSpace, infoButtonItem, rightSpace];
  93. }
  94. - (void)updateSelectionInfo
  95. {
  96. NSMutableOrderedSet *selectedAssets = self.imagePickerController.selectedAssets;
  97. if (selectedAssets.count > 0) {
  98. NSBundle *bundle = self.imagePickerController.assetBundle;
  99. NSString *format;
  100. if (selectedAssets.count > 1) {
  101. format = NSLocalizedStringFromTableInBundle(@"assets.toolbar.items-selected", @"QBImagePicker", bundle, nil);
  102. } else {
  103. format = NSLocalizedStringFromTableInBundle(@"assets.toolbar.item-selected", @"QBImagePicker", bundle, nil);
  104. }
  105. NSString *title = [NSString stringWithFormat:format, selectedAssets.count];
  106. [(UIBarButtonItem *)self.toolbarItems[1] setTitle:title];
  107. } else {
  108. [(UIBarButtonItem *)self.toolbarItems[1] setTitle:@""];
  109. }
  110. }
  111. #pragma mark - Fetching Asset Collections
  112. - (void)updateAssetCollections
  113. {
  114. // Filter albums
  115. NSArray *assetCollectionSubtypes = self.imagePickerController.assetCollectionSubtypes;
  116. NSMutableDictionary *smartAlbums = [NSMutableDictionary dictionaryWithCapacity:assetCollectionSubtypes.count];
  117. NSMutableArray *userAlbums = [NSMutableArray array];
  118. for (PHFetchResult *fetchResult in self.fetchResults) {
  119. [fetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *assetCollection, NSUInteger index, BOOL *stop) {
  120. PHAssetCollectionSubtype subtype = assetCollection.assetCollectionSubtype;
  121. if (subtype == PHAssetCollectionSubtypeAlbumRegular) {
  122. [userAlbums addObject:assetCollection];
  123. } else if ([assetCollectionSubtypes containsObject:@(subtype)]) {
  124. if (!smartAlbums[@(subtype)]) {
  125. smartAlbums[@(subtype)] = [NSMutableArray array];
  126. }
  127. [smartAlbums[@(subtype)] addObject:assetCollection];
  128. }
  129. }];
  130. }
  131. NSMutableArray *assetCollections = [NSMutableArray array];
  132. // Fetch smart albums
  133. for (NSNumber *assetCollectionSubtype in assetCollectionSubtypes) {
  134. NSArray *collections = smartAlbums[assetCollectionSubtype];
  135. if (collections) {
  136. [assetCollections addObjectsFromArray:collections];
  137. }
  138. }
  139. // Fetch user albums
  140. [userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *assetCollection, NSUInteger index, BOOL *stop) {
  141. [assetCollections addObject:assetCollection];
  142. }];
  143. self.assetCollections = assetCollections;
  144. }
  145. - (UIImage *)placeholderImageWithSize:(CGSize)size
  146. {
  147. UIGraphicsBeginImageContext(size);
  148. CGContextRef context = UIGraphicsGetCurrentContext();
  149. UIColor *backgroundColor = [UIColor colorWithRed:(239.0 / 255.0) green:(239.0 / 255.0) blue:(244.0 / 255.0) alpha:1.0];
  150. UIColor *iconColor = [UIColor colorWithRed:(179.0 / 255.0) green:(179.0 / 255.0) blue:(182.0 / 255.0) alpha:1.0];
  151. // Background
  152. CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
  153. CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
  154. // Icon (back)
  155. CGRect backIconRect = CGRectMake(size.width * (16.0 / 68.0),
  156. size.height * (20.0 / 68.0),
  157. size.width * (32.0 / 68.0),
  158. size.height * (24.0 / 68.0));
  159. CGContextSetFillColorWithColor(context, [iconColor CGColor]);
  160. CGContextFillRect(context, backIconRect);
  161. CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
  162. CGContextFillRect(context, CGRectInset(backIconRect, 1.0, 1.0));
  163. // Icon (front)
  164. CGRect frontIconRect = CGRectMake(size.width * (20.0 / 68.0),
  165. size.height * (24.0 / 68.0),
  166. size.width * (32.0 / 68.0),
  167. size.height * (24.0 / 68.0));
  168. CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
  169. CGContextFillRect(context, CGRectInset(frontIconRect, -1.0, -1.0));
  170. CGContextSetFillColorWithColor(context, [iconColor CGColor]);
  171. CGContextFillRect(context, frontIconRect);
  172. CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
  173. CGContextFillRect(context, CGRectInset(frontIconRect, 1.0, 1.0));
  174. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  175. UIGraphicsEndImageContext();
  176. return image;
  177. }
  178. #pragma mark - Checking for Selection Limit
  179. - (BOOL)isMinimumSelectionLimitFulfilled
  180. {
  181. return (self.imagePickerController.minimumNumberOfSelection <= self.imagePickerController.selectedAssets.count);
  182. }
  183. - (BOOL)isMaximumSelectionLimitReached
  184. {
  185. NSUInteger minimumNumberOfSelection = MAX(1, self.imagePickerController.minimumNumberOfSelection);
  186. if (minimumNumberOfSelection <= self.imagePickerController.maximumNumberOfSelection) {
  187. return (self.imagePickerController.maximumNumberOfSelection <= self.imagePickerController.selectedAssets.count);
  188. }
  189. return NO;
  190. }
  191. - (void)updateControlState
  192. {
  193. self.doneButton.enabled = [self isMinimumSelectionLimitFulfilled];
  194. }
  195. #pragma mark - UITableViewDataSource
  196. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  197. {
  198. return 1;
  199. }
  200. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  201. {
  202. return self.assetCollections.count;
  203. }
  204. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  205. {
  206. QBAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AlbumCell" forIndexPath:indexPath];
  207. cell.tag = indexPath.row;
  208. cell.borderWidth = 1.0 / self.traitCollection.displayScale;
  209. // Thumbnail
  210. PHAssetCollection *assetCollection = self.assetCollections[indexPath.row];
  211. PHFetchOptions *options = [PHFetchOptions new];
  212. switch (self.imagePickerController.mediaType) {
  213. case QBImagePickerMediaTypeImage:
  214. options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
  215. break;
  216. case QBImagePickerMediaTypeVideo:
  217. options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo];
  218. break;
  219. default:
  220. break;
  221. }
  222. PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
  223. PHImageManager *imageManager = [PHImageManager defaultManager];
  224. if (fetchResult.count >= 3) {
  225. cell.imageView3.hidden = NO;
  226. [imageManager requestImageForAsset:fetchResult[fetchResult.count - 3]
  227. targetSize:CGSizeScale(cell.imageView3.frame.size, self.traitCollection.displayScale)
  228. contentMode:PHImageContentModeAspectFill
  229. options:nil
  230. resultHandler:^(UIImage *result, NSDictionary *info) {
  231. if (cell.tag == indexPath.row) {
  232. cell.imageView3.image = result;
  233. }
  234. }];
  235. } else {
  236. cell.imageView3.hidden = YES;
  237. }
  238. if (fetchResult.count >= 2) {
  239. cell.imageView2.hidden = NO;
  240. [imageManager requestImageForAsset:fetchResult[fetchResult.count - 2]
  241. targetSize:CGSizeScale(cell.imageView2.frame.size, self.traitCollection.displayScale)
  242. contentMode:PHImageContentModeAspectFill
  243. options:nil
  244. resultHandler:^(UIImage *result, NSDictionary *info) {
  245. if (cell.tag == indexPath.row) {
  246. cell.imageView2.image = result;
  247. }
  248. }];
  249. } else {
  250. cell.imageView2.hidden = YES;
  251. }
  252. if (fetchResult.count >= 1) {
  253. [imageManager requestImageForAsset:fetchResult[fetchResult.count - 1]
  254. targetSize:CGSizeScale(cell.imageView1.frame.size, self.traitCollection.displayScale)
  255. contentMode:PHImageContentModeAspectFill
  256. options:nil
  257. resultHandler:^(UIImage *result, NSDictionary *info) {
  258. if (cell.tag == indexPath.row) {
  259. cell.imageView1.image = result;
  260. }
  261. }];
  262. }
  263. if (fetchResult.count == 0) {
  264. cell.imageView3.hidden = NO;
  265. cell.imageView2.hidden = NO;
  266. // Set placeholder image
  267. UIImage *placeholderImage = [self placeholderImageWithSize:cell.imageView1.frame.size];
  268. cell.imageView1.image = placeholderImage;
  269. cell.imageView2.image = placeholderImage;
  270. cell.imageView3.image = placeholderImage;
  271. }
  272. // Album title
  273. cell.titleLabel.text = assetCollection.localizedTitle;
  274. // Number of photos
  275. cell.countLabel.text = [NSString stringWithFormat:@"%lu", (long)fetchResult.count];
  276. return cell;
  277. }
  278. #pragma mark - PHPhotoLibraryChangeObserver
  279. - (void)photoLibraryDidChange:(PHChange *)changeInstance
  280. {
  281. dispatch_async(dispatch_get_main_queue(), ^{
  282. // Update fetch results
  283. NSMutableArray *fetchResults = [self.fetchResults mutableCopy];
  284. [self.fetchResults enumerateObjectsUsingBlock:^(PHFetchResult *fetchResult, NSUInteger index, BOOL *stop) {
  285. PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:fetchResult];
  286. if (changeDetails) {
  287. [fetchResults replaceObjectAtIndex:index withObject:changeDetails.fetchResultAfterChanges];
  288. }
  289. }];
  290. if (![self.fetchResults isEqualToArray:fetchResults]) {
  291. self.fetchResults = fetchResults;
  292. // Reload albums
  293. [self updateAssetCollections];
  294. [self.tableView reloadData];
  295. }
  296. });
  297. }
  298. @end