// // UMImagePickerController.m // UMCommunity // // Created by luyiyuan on 14/9/9. // Copyright (c) 2014年 Umeng. All rights reserved. // #import "UMImagePickerController.h" #import "UMImagePickerGroupCell.h" #import "UMAssetsCollectionController.h" #import "UMComResouceDefines.h" #import #import "UIViewController+UMComAddition.h" #import #import static NSArray *groupTypes; @interface UMImagePickerController () @property (nonatomic, strong) ALAssetsLibrary *assetsLibrary; @property (nonatomic, copy, readwrite) NSArray *assetsGroups; //返回的image @property (nonatomic,strong) UIImage* backImage; @end @implementation UMImagePickerController { UIView *footView; } + (BOOL)isAccessible { return ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] && [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]); } - (id)init { self = [super init]; if(self) { [self setUpProperties]; } return self; } - (id)initWithBackImage:(UIImage*)image { if (self = [super init]) { self.backImage = image; [self setUpProperties]; } return self; } - (void)setUpProperties { self.assetsLibrary = [[ALAssetsLibrary alloc] init]; if ([[UIDevice currentDevice].systemVersion floatValue] < 7) { [self.navigationController.navigationBar setBackgroundColor:UMComColorWithHexString(@"#f7f7f8")]; [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault]; }else{ [self.navigationController.navigationBar setBarTintColor:UMComColorWithHexString(@"#f7f7f8")]; } // Register cell classes [self.tableView registerClass:[UMImagePickerGroupCell class] forCellReuseIdentifier:@"GroupCell"]; if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) { UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)]; [self.navigationItem setLeftBarButtonItem:cancelButton animated:NO]; }else{ if (self.backImage) { [self setForumUIBackButtonWithImage:self.backImage]; } else{ [self setBackButtonWithTitle:UMComLocalizedString(@"um_com_cancel", @"取消")]; } } } - (void)cancel:(id)sender { [self dismissViewControllerAnimated:YES completion:^{ }]; if(self.finishHandle) { self.finishHandle(YES,nil); } } - (void)done:(NSArray *)assets { [self dismissViewControllerAnimated:YES completion:^{ }]; if(self.finishHandle) { self.finishHandle(NO,assets); } } - (void)loadAssetsGroup:(void (^)(NSArray *assetsGroups))completion { if(!groupTypes) { groupTypes = @[@(ALAssetsGroupSavedPhotos), @(ALAssetsGroupPhotoStream), @(ALAssetsGroupAlbum)]; } __block NSMutableArray *assetsGroups = [NSMutableArray array]; __block NSUInteger numberOfFinishedTypes = 0; for (NSNumber *type in groupTypes) { [self.assetsLibrary enumerateGroupsWithTypes:[type unsignedIntegerValue] usingBlock:^(ALAssetsGroup *assetsGroup, BOOL *stop) { if (assetsGroup) { // Filter the assets group [assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]]; if (assetsGroup.numberOfAssets > 0) { // Add assets group [assetsGroups addObject:assetsGroup]; } } else { numberOfFinishedTypes++; } // Check if the loading finished if (numberOfFinishedTypes == groupTypes.count) { // Sort assets groups NSArray *sortedAssetsGroups = [self sortAssetsGroups:(NSArray *)assetsGroups typesOrder:groupTypes]; // Call completion block SafeCompletionData(completion, sortedAssetsGroups) } } failureBlock:^(NSError *error) { UMLog(@"Error: %@", [error localizedDescription]); }]; } } - (NSArray *)sortAssetsGroups:(NSArray *)assetsGroups typesOrder:(NSArray *)typesOrder { NSMutableArray *sortedAssetsGroups = [NSMutableArray array]; for (ALAssetsGroup *assetsGroup in assetsGroups) { if (sortedAssetsGroups.count == 0) { [sortedAssetsGroups addObject:assetsGroup]; continue; } ALAssetsGroupType assetsGroupType = [[assetsGroup valueForProperty:ALAssetsGroupPropertyType] unsignedIntegerValue]; NSUInteger indexOfAssetsGroupType = [typesOrder indexOfObject:@(assetsGroupType)]; for (NSInteger i = 0; i <= sortedAssetsGroups.count; i++) { if (i == sortedAssetsGroups.count) { [sortedAssetsGroups addObject:assetsGroup]; break; } ALAssetsGroup *sortedAssetsGroup = sortedAssetsGroups[i]; ALAssetsGroupType sortedAssetsGroupType = [[sortedAssetsGroup valueForProperty:ALAssetsGroupPropertyType] unsignedIntegerValue]; NSUInteger indexOfSortedAssetsGroupType = [typesOrder indexOfObject:@(sortedAssetsGroupType)]; if (indexOfAssetsGroupType < indexOfSortedAssetsGroupType) { [sortedAssetsGroups insertObject:assetsGroup atIndex:i]; break; } } } return [sortedAssetsGroups copy]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Load assets groups } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; // footView = [[UIView alloc]initWithFrame:CGRectMake(0, 1, self.tableView.frame.size.width, BottomLineHeight)]; if ([[[UIDevice currentDevice] systemVersion]floatValue] < 8.0) { footView.backgroundColor = UMComTableViewSeparatorColor; }else{ footView.backgroundColor = [UIColor clearColor]; } self.tableView.tableFooterView = footView; if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:UIEdgeInsetsZero]; } [self loadAssetsGroup:^(NSArray *assetsGroups) { self.assetsGroups = assetsGroups; [self.tableView reloadData]; }]; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return self.assetsGroups.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UMImagePickerGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; ALAssetsGroup *assetsGroup = self.assetsGroups[indexPath.row]; cell.assetsGroup = assetsGroup; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 86.0; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UMAssetsCollectionController *assetsCollectionViewController = [[UMAssetsCollectionController alloc] init]; assetsCollectionViewController.backImage = self.backImage; assetsCollectionViewController.minimumNumberOfSelection = self.minimumNumberOfSelection; assetsCollectionViewController.maximumNumberOfSelection = self.maximumNumberOfSelection; assetsCollectionViewController.assetsGroup = self.assetsGroups[indexPath.row]; [assetsCollectionViewController setFinishHandle:^(NSArray *assets){ [self done:assets]; }]; [self.navigationController pushViewController:assetsCollectionViewController animated:YES]; } /* // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */ /* // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } */ /* // Override to support conditional rearranging of the table view. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */ /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end