Ios album operation ALAssetsLibrary

Source: Internet
Author: User

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;}
 









Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.