白衣染霜花
-(void)getFromGallery:(BOOL )IsImages{ if(self.csCollectionsArray != nil) [self.csCollectionsArray removeAllObjects]; __block NSMutableDictionary *date = [[NSMutableDictionary alloc] init]; ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init]; NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos; [csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if([group numberOfAssets] > 0) { if(IsImages) [group setAssetsFilter:[ALAssetsFilter allPhotos]]; else [group setAssetsFilter:[ALAssetsFilter allVideos]]; [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) { if(asset) { //1.fetching all assets from device library //2.Add all fetched assests from library [date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]]; } }]; } else { NSLog(@"---> load table -------->"); if(date != nil && date.count > 0) { //3.Sort using date by ascending order and moved to dictionary to array NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: @selector(compare:)]; for (NSString *key in sortedKeys) [self.csCollectionsArray addObject: [date objectForKey:key]]; //4.Load images into collection view after fetching all datas [self reloadCollectionView]; if(self.csCollectionView != nil) [self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES]; } date = nil; } }failureBlock:^(NSError *error) { if((csCollectionsArray == nil || [csCollectionsArray count] == 0)) { ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized) { [self showAlertAndCloseUploaderView:@"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"]; } } }];}您可以使用以下方法从ios中的资产库中获取所有图像或视频。使用此资产库框架(必须)注:-#import-(void)getFromGallery:(BOOL )IsImages{ if(self.csCollectionsArray != nil) [self.csCollectionsArray removeAllObjects]; __block NSMutableDictionary *date = [[NSMutableDictionary alloc] init]; ALAssetsLibrary *csAssetsLibrary = [[ALAssetsLibrary alloc] init]; NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces | ALAssetsGroupSavedPhotos; [csAssetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if([group numberOfAssets] > 0) { if(IsImages) [group setAssetsFilter:[ALAssetsFilter allPhotos]]; else [group setAssetsFilter:[ALAssetsFilter allVideos]]; [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) { if(asset) { //1.fetching all assets from device library //2.Add all fetched assests from library [date setObject:asset forKey:[asset valueForProperty:ALAssetPropertyDate]]; } }]; } else { NSLog(@"---> load table -------->"); if(date != nil && date.count > 0) { //3.Sort using date by ascending order and moved to dictionary to array NSArray *sortedKeys = [[date allKeys] sortedArrayUsingSelector: @selector(compare:)]; for (NSString *key in sortedKeys) [self.csCollectionsArray addObject: [date objectForKey:key]]; //4.Load images into collection view after fetching all datas [self reloadCollectionView]; if(self.csCollectionView != nil) [self.csCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:([self.csCollectionsArray count] - 1) inSection:0] atScrollPosition:UICollectionViewScrollPositionBottom animated:YES]; } date = nil; } }failureBlock:^(NSError *error) { if((csCollectionsArray == nil || [csCollectionsArray count] == 0)) { ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; if(status != ALAuthorizationStatusAuthorized) { [self showAlertAndCloseUploaderView:@"You can just go to \"Settings\" app (General -> Reset -> Reset Location & Privacy) then come again and click ok when the alert dialog is showing for enable the permission to access the photo library"]; } } }];}