123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- /*
- Copyright (c) 2013 Katsuma Tanaka
-
- 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:
-
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
- 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.
- */
- #import "QBImagePickerController.h"
- // Views
- #import "QBImagePickerGroupCell.h"
- // Controllers
- #import "QBAssetCollectionViewController.h"
- @interface QBImagePickerController ()
- @property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
- @property (nonatomic, strong) NSMutableArray *assetsGroups;
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, assign) UIBarStyle previousBarStyle;
- @property (nonatomic, assign) BOOL previousBarTranslucent;
- @property (nonatomic, assign) UIStatusBarStyle previousStatusBarStyle;
- @property (nonatomic, assign) UIView *navigationBarView;
- - (void)cancel;
- - (NSDictionary *)mediaInfoFromAsset:(ALAsset *)asset;
- @end
- @implementation QBImagePickerController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
-
- if(self) {
- /* Check sources */
- [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
- [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
- [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
-
- /* Initialization */
- self.title = @"Photos";
- self.filterType = QBImagePickerFilterTypeAllPhotos;
- self.showsCancelButton = YES;
- self.fullScreenLayoutEnabled = YES;
-
- self.allowsMultipleSelection = NO;
- self.limitsMinimumNumberOfSelection = NO;
- self.limitsMaximumNumberOfSelection = NO;
- self.minimumNumberOfSelection = 0;
- self.maximumNumberOfSelection = 0;
-
- ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
- self.assetsLibrary = assetsLibrary;
- // [assetsLibrary release];
-
- self.assetsGroups = [NSMutableArray array];
-
- // Table View
- UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT - JX_SCREEN_TOP) style:UITableViewStylePlain];
- tableView.dataSource = self;
- tableView.delegate = self;
- tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- [self.view addSubview:tableView];
- self.tableView = tableView;
- self.tableView.estimatedRowHeight = 0;
- self.tableView.estimatedSectionFooterHeight = 0;
- self.tableView.estimatedSectionHeaderHeight = 0;
- // [tableView release];
- }
-
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- void (^assetsGroupsEnumerationBlock)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *assetsGroup, BOOL *stop) {
- if(assetsGroup) {
- switch(self.filterType) {
- case QBImagePickerFilterTypeAllAssets:
- [assetsGroup setAssetsFilter:[ALAssetsFilter allAssets]];
- break;
- case QBImagePickerFilterTypeAllPhotos:
- [assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
- break;
- case QBImagePickerFilterTypeAllVideos:
- [assetsGroup setAssetsFilter:[ALAssetsFilter allVideos]];
- break;
- }
-
- if(assetsGroup.numberOfAssets > 0) {
- [self.assetsGroups addObject:assetsGroup];
- [self.tableView reloadData];
- }
- }
- };
-
- void (^assetsGroupsFailureBlock)(NSError *) = ^(NSError *error) {
- NSLog(@"Error: %@", [error localizedDescription]);
- };
-
- // Enumerate Camera Roll
- [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
-
- // Photo Stream
- [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupPhotoStream usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
-
- // Album
- [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
-
- // Event
- [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupEvent usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
-
- // Faces
- [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupFaces usingBlock:assetsGroupsEnumerationBlock failureBlock:assetsGroupsFailureBlock];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
-
- self.navigationController.navigationBar.hidden = YES;
- [self createNavgationBar];
- self.showsCancelButton = YES;
- // Full screen layout
- if(self.fullScreenLayoutEnabled) {
- NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
-
- if(indexPath == nil) {
- self.previousBarStyle = self.navigationController.navigationBar.barStyle;
- self.previousBarTranslucent = self.navigationController.navigationBar.translucent;
- self.previousStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
-
- self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
- self.navigationController.navigationBar.translucent = YES;
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
-
- // CGFloat top = 0;
- // if(![[UIApplication sharedApplication] isStatusBarHidden]) top = top + 20;
- // if(!self.navigationController.navigationBarHidden) top = top + 44;
- // self.tableView.contentInset = UIEdgeInsetsMake(top, 0, 0, 0);
- // self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(top, 0, 0, 0);
- //
- // [self setWantsFullScreenLayout:YES];
- }
- }
-
- // Cancel table view selection
- [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
- }
- - (void) createNavgationBar {
-
- UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_TOP)];
- if (THESIMPLESTYLE) {
- view.backgroundColor = [UIColor whiteColor];
- }else {
- view.backgroundColor = THEMECOLOR;
- }
- [self.view addSubview:view];
-
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP - 30, 200, 20)];
- label.font = [UIFont systemFontOfSize:17];
- label.textColor = [UIColor whiteColor];
- label.textAlignment = NSTextAlignmentCenter;
- label.text = @"photos";
- label.center = CGPointMake(view.frame.size.width / 2, label.center.y);
- [view addSubview:label];
-
- self.navigationBarView = view;
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
-
- // Flash scroll indicators
- [self.tableView flashScrollIndicators];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
-
- // Restore bar styles
- self.navigationController.navigationBar.barStyle = self.previousBarStyle;
- self.navigationController.navigationBar.translucent = self.previousBarTranslucent;
- [[UIApplication sharedApplication] setStatusBarStyle:self.previousStatusBarStyle animated:YES];
- }
- - (void)setShowsCancelButton:(BOOL)showsCancelButton
- {
- _showsCancelButton = showsCancelButton;
-
- if(self.showsCancelButton) {
-
- UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH -90, JX_SCREEN_TOP - 34, 80, 25)];
- [btn setTitle:Localized(@"JX_Cencal") forState:UIControlStateNormal];
- btn.titleLabel.font = [UIFont systemFontOfSize:17];
- [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
- [self.navigationBarView addSubview:btn];
-
- // UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
- // [self.navigationItem setRightBarButtonItem:cancelButton animated:NO];
- // [cancelButton release];
- } else {
- [self.navigationItem setRightBarButtonItem:nil animated:NO];
- }
- }
- - (void)dealloc
- {
- // [_assetsLibrary release];
- // [_assetsGroups release];
- //
- // [_tableView release];
- //
- // [super dealloc];
- }
- #pragma mark - Instance Methods
- - (void)cancel
- {
- if([self.delegate respondsToSelector:@selector(imagePickerControllerDidCancel:)]) {
- [self.delegate imagePickerControllerDidCancel:self];
- }
- }
- - (NSDictionary *)mediaInfoFromAsset:(ALAsset *)asset
- {
- NSMutableDictionary *mediaInfo = [NSMutableDictionary dictionary];
- [mediaInfo setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
- [mediaInfo setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
- [mediaInfo setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];
-
- return mediaInfo;
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return self.assetsGroups.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *cellIdentifier = @"Cell";
- QBImagePickerGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
-
- if(cell == nil) {
- cell = [[QBImagePickerGroupCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
- cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
- }
-
- ALAssetsGroup *assetsGroup = [self.assetsGroups objectAtIndex:indexPath.row];
-
- cell.imageView.image = [UIImage imageWithCGImage:assetsGroup.posterImage];
- cell.titleLabel.text = [NSString stringWithFormat:@"%@", [assetsGroup valueForProperty:ALAssetsGroupPropertyName]];
- cell.countLabel.text = [NSString stringWithFormat:@"(%ld)", assetsGroup.numberOfAssets];
-
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 60;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- ALAssetsGroup *assetsGroup = [self.assetsGroups objectAtIndex:indexPath.row];
-
- BOOL showsHeaderButton = ([self.delegate respondsToSelector:@selector(descriptionForSelectingAllAssets:)] && [self.delegate respondsToSelector:@selector(descriptionForDeselectingAllAssets:)]);
-
- BOOL showsFooterDescription = NO;
-
- switch(self.filterType) {
- case QBImagePickerFilterTypeAllAssets:
- showsFooterDescription = ([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:numberOfVideos:)]);
- break;
- case QBImagePickerFilterTypeAllPhotos:
- showsFooterDescription = ([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:)]);
- break;
- case QBImagePickerFilterTypeAllVideos:
- showsFooterDescription = ([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfVideos:)]);
- break;
- }
-
- // Show assets collection view
- QBAssetCollectionViewController *assetCollectionViewController = [[QBAssetCollectionViewController alloc] init];
- assetCollectionViewController.title = [assetsGroup valueForProperty:ALAssetsGroupPropertyName];
- assetCollectionViewController.delegate = self;
- assetCollectionViewController.assetsGroup = assetsGroup;
- assetCollectionViewController.filterType = self.filterType;
- assetCollectionViewController.showsCancelButton = self.showsCancelButton;
- assetCollectionViewController.fullScreenLayoutEnabled = self.fullScreenLayoutEnabled;
- assetCollectionViewController.showsHeaderButton = showsHeaderButton;
- assetCollectionViewController.showsFooterDescription = showsFooterDescription;
-
- assetCollectionViewController.allowsMultipleSelection = self.allowsMultipleSelection;
- assetCollectionViewController.limitsMinimumNumberOfSelection = self.limitsMinimumNumberOfSelection;
- assetCollectionViewController.limitsMaximumNumberOfSelection = self.limitsMaximumNumberOfSelection;
- assetCollectionViewController.minimumNumberOfSelection = self.minimumNumberOfSelection;
- assetCollectionViewController.maximumNumberOfSelection = self.maximumNumberOfSelection;
-
- [self.navigationController pushViewController:assetCollectionViewController animated:YES];
-
- // [assetCollectionViewController release];
- }
- #pragma mark - QBAssetCollectionViewControllerDelegate
- - (void)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController didFinishPickingAsset:(ALAsset *)asset
- {
- if([self.delegate respondsToSelector:@selector(imagePickerControllerWillFinishPickingMedia:)]) {
- [self.delegate imagePickerControllerWillFinishPickingMedia:self];
- }
-
- if([self.delegate respondsToSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)]) {
- [self.delegate imagePickerController:self didFinishPickingMediaWithInfo:[self mediaInfoFromAsset:asset]];
- }
- }
- - (void)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController didFinishPickingAssets:(NSArray *)assets
- {
- if([self.delegate respondsToSelector:@selector(imagePickerControllerWillFinishPickingMedia:)]) {
- [self.delegate imagePickerControllerWillFinishPickingMedia:self];
- }
-
- if([self.delegate respondsToSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)]) {
- NSMutableArray *info = [NSMutableArray array];
-
- for(ALAsset *asset in assets) {
- [info addObject:[self mediaInfoFromAsset:asset]];
- }
-
- [self.delegate imagePickerController:self didFinishPickingMediaWithInfo:info];
- }
- }
- - (void)assetCollectionViewControllerDidCancel:(QBAssetCollectionViewController *)assetCollectionViewController
- {
- if([self.delegate respondsToSelector:@selector(imagePickerControllerDidCancel:)]) {
- [self.delegate imagePickerControllerDidCancel:self];
- }
- }
- - (NSString *)descriptionForSelectingAllAssets:(QBAssetCollectionViewController *)assetCollectionViewController
- {
- NSString *description = nil;
-
- if([self.delegate respondsToSelector:@selector(descriptionForSelectingAllAssets:)]) {
- description = [self.delegate descriptionForSelectingAllAssets:self];
- }
-
- return description;
- }
- - (NSString *)descriptionForDeselectingAllAssets:(QBAssetCollectionViewController *)assetCollectionViewController
- {
- NSString *description = nil;
-
- if([self.delegate respondsToSelector:@selector(descriptionForDeselectingAllAssets:)]) {
- description = [self.delegate descriptionForDeselectingAllAssets:self];
- }
-
- return description;
- }
- - (NSString *)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController descriptionForNumberOfPhotos:(NSUInteger)numberOfPhotos
- {
- NSString *description = nil;
-
- if([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:)]) {
- description = [self.delegate imagePickerController:self descriptionForNumberOfPhotos:numberOfPhotos];
- }
-
- return description;
- }
- - (NSString *)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController descriptionForNumberOfVideos:(NSUInteger)numberOfVideos
- {
- NSString *description = nil;
-
- if([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfVideos:)]) {
- description = [self.delegate imagePickerController:self descriptionForNumberOfVideos:numberOfVideos];
- }
-
- return description;
- }
- - (NSString *)assetCollectionViewController:(QBAssetCollectionViewController *)assetCollectionViewController descriptionForNumberOfPhotos:(NSUInteger)numberOfPhotos numberOfVideos:(NSUInteger)numberOfVideos
- {
- NSString *description = nil;
-
- if([self.delegate respondsToSelector:@selector(imagePickerController:descriptionForNumberOfPhotos:numberOfVideos:)]) {
- description = [self.delegate imagePickerController:self descriptionForNumberOfPhotos:numberOfPhotos numberOfVideos:numberOfVideos];
- }
-
- return description;
- }
- @end
|