Ios album operation ALAssetsLibrary
1. The ALAssetsLibrary instance provides us with the function of getting pictures and videos in an album (photo app. In ios8 photos framework, ALAssetsLibrary is replaced.
When using ALAssetsLibrary, We need to declare its instance.
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
2. Obtain the album ALAssetsGroup iteratively:
- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock
ALASSetsGroupType: Type ALAssetsGroupLibrary: album content from iTunes (for example, self-contained sunflower photos ). ALAssetsGroupAlbum: A photo generated by the device itself or synchronized from iTunes, but not included in the photo stream and sharing stream. (For example, images saved from various software) album generated by ALAssetsGroupEvent camera interface event
ALAssetsGroupFaces face album (Unclear)
ALAssetsGroupSavedPhotos camera film photo
ALAssetsGroupPhotoStream photo stream
ALAssetsGroupAll except the content above ALAssetsGroupLibrary.
ALAssetsLibraryGroupsEnumerationResultsBlock
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) { ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos]; [group setAssetsFilter:onlyPhotosFilter]; if ([group numberOfAssets] > 0) { [self.imageGroup addObject:group]; } else { [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; } };
The above is the block for iteration of AlAssetsGroup. Each iteration stores the corresponding AlAssetsGroup in a variable array. Some attributes in AlAssetsGroup indicate the characteristics of the album. For example:
Thumbnail of A posterImage album
Number of photos in numberOfAssets album
ValueForProperty obtains some attributes through NSString. the following attributes are available:
NSString * constALAssetsGroupPropertyName; NSString * constALAssetsGroupPropertyType; NSString * constALAssetsGroupPropertyPersistentID; (unique ID) NSString * constALAssetsGroupPropertyURL; (unique URL
#import "AssetsViewController.h"#import @interface AssetsViewController ()
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) NSMutableArray *imageGroup;@end@implementation AssetsViewController- (ALAssetsLibrary *)assetsLibrary{ if (!_assetsLibrary) { _assetsLibrary = [[ALAssetsLibrary alloc] init]; } return _assetsLibrary;}- (NSMutableArray *)imageGroup{ if (!_imageGroup) { _imageGroup = [[NSMutableArray alloc] initWithCapacity:0]; } return _imageGroup;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.imageGroup removeAllObjects]; UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; [self.view addSubview:tableView]; tableView.delegate = self; tableView.dataSource = self; ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) { ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos]; [group setAssetsFilter:onlyPhotosFilter]; if ([group numberOfAssets] > 0) { [self.imageGroup addObject:group]; } else { [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; } }; NSUInteger groupTypes = ALAssetsGroupAll ; [self.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:listGroupBlock failureBlock:^(NSError *error) { NSLog(@"Group not found!\n"); }];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.imageGroup.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellName = @"name"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName]; } ALAssetsGroup *group = [self.imageGroup objectAtIndex:indexPath.row]; UIImage *postImage = [UIImage imageWithCGImage:[group posterImage]]; cell.imageView.image = postImage; cell.textLabel.text = [group valueForProperty:ALAssetsGroupPropertyName];; cell.detailTextLabel.text = [@(group.numberOfAssets) stringValue]; return cell;}