Alassetslibrary provides access to all photos and videos under the Photos app under the iOS device.
All the album data can be read from alassetslibrary, i.e. the Alassetsgroup object list;
From each alassetsgroup, you get a list of the photos or videos that are included in the Alasset object list.
Its hierarchical relationship is Alassetrepresentation alassetslibrary, Alassetsgroup, Alasset.
1. Each alasset may have multiple representations representations, namely Alassetrepresentation objects:
2. Use its Defaultrepresentation method to obtain its default representations,
3. Use [asset valueforproperty:alassetpropertyrepresentations] to get the UTI array for all of its representations.
4. The thumbnail thumbnail or aspectratiothumbnail can be obtained from the Alasset object;
5. The full-size picture (fullresolutionimage), full-screen picture (fullscreenimage) and the various properties of the picture can be obtained from the Alassetrepresentation object: orientation, dimensions, scale, URL, metadata and so on.
The following code and its related comments:
#import <AssetsLibrary/AssetsLibrary.h> Uiimageview*imageview = [[UiimageviewAlloc] Initwithframe:cgrectmake (Ten, -, Self. View. Frame. Size. Width- -, Self. View. Frame. Size. Height- -)]; [ Self. ViewAddsubview:imageview]; Alassetslibrary *assetslibrary = [[Alassetslibrary alloc] init];//Asynchronous//Traverse Assetsgroup[Assetslibrary enumerategroupswithtypes:alassetsgroupall usingblock:^ (Alassetsgroup *group,BOOL*stop) {if(group) {NSLog(@"group:%@", group);//Cover picture UIImage*cover = [[UIImageAlloc] Initwithcgimage:[group Posterimage];//imageview.image = cover; ///sync method, only all assets traversal is completed before returning. So prevent blocking the UI thread. //traverse the assets in the assets group //[group Setassetsfilter:[alassetsfilter Allphotos]];[Group enumerateassetsusingblock:^ (Alasset *asset, Nsuinteger Index,BOOL*stop) {if(asset) {NSLog(@"\nasset:%@", asset);NSLog(@"Alassetpropertyasseturl:%@", [asset Valueforproperty:alassetpropertyasseturl]);UIImage*image = [[UIImageAlloc] Initwithcgimage:[[asset defaultrepresentation] fullscreenimage];//URL NSString*url = [[[Asset defaultrepresentation] url] absolutestring];NSLog(@"URL:%@", URL);//thumbnail image UIImage*thumbnail = [[UIImageAlloc] Initwithcgimage:[asset thumbnail];UIImage*aspectratiothumbnail = [[UIImageAlloc] Initwithcgimage:[asset Aspectratiothumbnail];//Each alasset may have more than one representation representation, that is, the Alassetrepresentation object. //Get the UTI array for all representations Nsarray*utiarrays = [NsarrayArraywithobject:[asset Valueforproperty:alassetpropertyrepresentations]];NSLog(@"Utiarrays:%@", utiarrays);//full-size drawing UIImage*fullresolutionimage = [UIImageImagewithcgimage:[[asset defaultrepresentation] fullresolutionimage];//Fullscreen view UIImage*fullscreenimage = [UIImageImagewithcgimage:[[asset defaultrepresentation] fullscreenimage]; ImageView. Image= Fullresolutionimage;//Creation time NSString*createtime = (NSString*) [Asset valueforproperty:alassetpropertydate];NSLog(@"Createtime:%@", createtime);//Shooting location NSString*createlocation = (NSString*) [Asset valueforproperty:alassetpropertylocation];NSLog(@"createlocation:%@", createlocation);//Size cgsizeDimensions = [[Asset defaultrepresentation] dimensions];NSLog(@"Dimensions:%f-%f", Dimensions. Width, Dimensions. Height); } }]; }} failureblock:^ (Nserror*error) {NSLog(@"%@", error);}];
It is important to note that:
1. Other objects obtained through the Alassetslibrary object are valid only for the lifetime of the Alassetslibrary object, even if the Alassetslibrary object is destroyed, and the other objects obtained from it are no longer accessible. Therefore, the use of the above block method will be more convenient.
2. All obtained are CGIMAGEREF objects.
3. It is recommended to use Fullscreenimage as it has been adjusted. The fullresolutionimage size is too large to be adjusted and scaled by itself.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS---Access all photo information from your device using Alassetslibrary