系統的UIImagePickerController只能讓用戶選擇單圖片,而通常狀況下,咱們須要上傳多張圖片,這時應該能夠同時選擇多張圖片,不然用戶體驗會不好。所以多圖片選擇器就誕生了。git
在類庫中,蘋果爲咱們提供了一個AssetsLibrary.Framework的庫,能夠經過它獲取設備裏的圖片和視頻。在使用這個類庫時, 咱們須要導入,且<AssetsLibrary/AssetsLibrary.h>。而後咱們能夠使用它來獲取不一樣組的枚舉。github
1 void (^assetGroupEnumerator) 2 (struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop{ 3 if(group != nil) { 4 [assetGroups addObject:group]; 5 NSLog(@"Number of assets in group: %d", 6 [group numberOfAssets]); 7 } 8 }; 9 10 assetGroups = [[NSMutableArray alloc] init]; 11 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 12 NSUInteger groupTypes = ALAssetsGroupAll; 13 14 [library enumerateGroupsWithTypes:groupTypes 15 usingBlock:assetGroupEnumerator 16 failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}]; 17 18 NSLog(@"Asset groups: %@", assetGroups); 19 [library release];
1-8行,咱們聲明一個block,使用它來枚舉全部asset group,而後將全部獲取到的group放在NSMutableArray中。app
1 void (^assetEnumerator) 2 (struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { 3 if(result != nil) { 4 if(![assetURLDictionaries containsObject:[result valueForProperty:ALAssetPropertyURLs]]) { 5 if(![[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) { 6 [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]]; 7 [assets addObject:result]; 8 } 9 } 10 } 11 }; 12 13 assets = [[NSMutableArray alloc] init]; 14 assetURLDictionaries = [[NSMutableArray alloc] init]; 15 16 NSInteger indexOfExampleGallery = NSNotFound; 17 for (ALAssetsGroup *group in assetGroups) { 18 if ([[group valueForProperty:ALAssetsGroupPropertyName] 19 isEqualToString:@"ExampleGallery"]) 20 indexOfExampleGallery = [assetGroups indexOfObject:group]; 21 } 22 23 if (indexOfExampleGallery != NSNotFound) { 24 [[assetGroups objectAtIndex:indexOfExampleGallery] 25 enumerateAssetsUsingBlock:assetEnumerator]; 26 NSLog(@"Assets %@", assets); 27 } 28 else 29 NSLog(@"Gallery 'ExampleGallery' not found on device.");
代碼中的ELCImagePickerController就是使用這個庫來完成同時選擇多張圖片,固然視頻也能夠。ide
ELCImagePickerController的使用spa
1 ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:@"ELCAlbumPickerController" bundle:[NSBundle mainBundle]]; 2 ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController]; 3 [albumController setParent:elcPicker]; 4 [elcPicker setDelegate:self]; 5 6 ELCImagePickerDemoAppDelegate *app = (ELCImagePickerDemoAppDelegate *)[[UIApplication sharedApplication] delegate]; 7 [app.viewController presentModalViewController:elcPicker animated:YES];
原文鏈接code