IOS get System Album data (not a system album)

Source: Internet
Author: User

Framework:AssetsLibrary.framework

The main purpose is to obtain the data of the system album and display the photos in the System album.

1, create a new project;

2. Add assetslibrary.framework to the project.

3. Open the storyboard, drag and drop a collection view (Collection view) component into the controller, then drag and drop an image view to the default cell of Collection view, and modify the image view display photo in the properties panel as aspect Fill. And, in the Properties panel, set the default cell (Collectionviewcell) identifier fill in the cell;

4, the collection View data source rain Agent export (outlet) connected to the controller (on the collection view right-click, connect on the line, or in the controller's code manually set the line)

5. Add a new class to the project, the class name is MyCell, and inherit from Uicollectionviewcell.

6. In the storyboard, point the class of cell CollectionView to MyCell.

7, associate the ImageView with the code (even to MyCell), named ImageView.

8, import in the controller code

#import <AssetsLibrary/AssetsLibrary.h>

#import "MyCell.h"

And let this class conform to

Uicollectionviewdatasource,uicollectionviewdelegate the specification of the Protocol and then declares two variables

{

Alassetslibrary *library;

Nsmutablearray *imagearr;

}

and link the Uicollectionview to the code. Named as Collview;

-(void) Viewdidload {

[Super Viewdidload];

Library = [[Alassetslibrary alloc]init];

Use parameters to remove all stored file photos

[Library Enumerategroupswithtypes:alassetsgroupsavedphotos usingblock:^ (Alassetsgroup *group, BOOL *stop) {

Nsmutablearray *temparray = [[Nsmutablearray alloc]init];

if (group! = nil) {

[Group enumerateassetsusingblock:^ (Alasset *result, Nsuinteger index, BOOL *stop) {

if (result = nil) {

[Temparray Addobject:result];

}

}];

Save Results

Imagearr = [Temparray copy];

NSLog (@ "Take out photos%lu Zhang", (unsigned long) imagearr.count);

[Self.collview Reloaddata];

}

} failureblock:^ (Nserror *error) {

Failed to read photo

}];

}

#pragma markuicollectionviewdatasource uicollectionviewdelegate

-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{

return 1;

}

-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section{

return imagearr.count;

}

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{

static NSString *identifier = @ "Cell";

MyCell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:identifier Forindexpath:indexpath];

if (cell = = nil) {

cell = [[MyCell alloc]init];

}

Remove data from each photo and convert to uiimage format

Cgimageref img = [[Imagearr objectAtIndex:indexPath.row] thumbnail];

Cell.imageView.image = [UIImage imagewithcgimage:img];

return cell;

}

IOS get System Album data (not a system album)

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.