Introduction: Photos Framework is a new iOS8.0 after the introduction of the system album for the relevant operations, before iOS8.0, development can only use the Assetslibrary framework to access the mobile device picture library. Assetslibrary is no longer introduced in this article and is described in detail only for the photos framework. and the 美图秀秀 photo selector is used as an example carrier to realize the function.
To get all the albums of the system first, there are several ways to choose
1. This method obtains the album phfetchoptions by the unique identifier identifiers to the related Property object that will get the album
+ (phfetchresult<phassetcollection *> *) fetchassetcollectionswithlocalidentifiers: (NSArray<NSString *> *) Identifiers Options: (Nullable phfetchoptions *) options;
2. This method obtains the related album by Phassetcollectiontype the album classification enumeration value Phassetcollectionsubtype is the subtype namely further wants the album classification such as album and Albumcoundshared combination means albums synced from itunes and shared from icloud
+ (phfetchresult<phassetcollection *> *) Fetchassetcollectionswithtype: (phassetcollectiontype) Type subtype: ( Phassetcollectionsubtype) Subtype options: (Nullable phfetchoptions *) options;
The associated enumeration values are as follows
typedef ns_enum (Nsinteger, Phassetcollectiontype) { Phassetcollectiontypealbum = 1,//sync from itunes and user-built albums phassetcollectiontypesmartalbum = 2,//Camera Photo album Phassetcollectiontypemoment
enumPhassetcollectionsubtype:int { CaseAlbumregular//albums created by users in Photos, which is what I call a logical album CaseAlbumsyncedevent//use ITunes to sync events from Photos photo gallery or IPhoto photo Gallery. However, on itunes 12 and iOS 9.0 beta4, it is necessary to use albumsyncedalbum for this type of event album that is not available for synchronization. CaseAlbumsyncedfaces//use ITunes to sync people albums from Photos photo gallery or IPhoto photo Gallery. CaseAlbumsyncedalbum//did what albumsyncedevent should do . Casealbumimported//The album imported from camera or external storage has no experience in this area and cannot be verified. CaseAlbummyphotostream//ICloud Photo Stream for users CaseAlbumcloudshared//albums shared by users using ICloud CaseSmartalbumgeneric//The document is interpreted as a non-special type of photo album, which mainly includes albums synced from IPhoto. Because my iPhoto has been replaced by Photos, it cannot be verified. However, on my IPad mini is not available, and the following types of albums, although not including photos or videos, but can get to. CaseSmartalbumpanoramas//panoramic photos taken by the camera CaseSmartalbumvideos//video taken by the camera CaseSmartalbumfavorites//Favorites Folder CaseSmartalbumtimelapses//time-lapse video folder, which also appears in the video folder CaseSmartalbumallhidden//a folder that contains hidden photos or videos Casesmartalbumrecentlyadded//recent photos or videos taken by the camera CaseSmartalbumbursts//shoot in burst mode, press and hold the shutter on the IPad Mini, but the photo is still not in the folder, but in the camera album. CaseSmartalbumslomovideos//Slomo is the abbreviation for slow motion, a slow motion resolution for high-speed photography, in which IOS devices are shot at 120 frames. But my IPad Mini is not supported and cannot be verified. CaseSmartalbumuserlibrary//This name is the most magical, the camera album, all the photos or videos taken by the camera will appear in the album, and other apps saved photos will appear here. CaseAny//contains all types}
3. This method is also through the album Classification for query usage ibid.
// Smart Albums is not supported, only Albums and Moments+ (phfetchresult<phassetcollection *> *) fetchassetc Ollectionscontainingasset: (Phasset *) Asset Withtype: (phassetcollectiontype) Type options: (Nullable phfetchoptions * ) options;
4. The method manages the unique identity URL of the assetslibrary before iOS8.0
+ (phfetchresult<phassetcollection *> *) Fetchassetcollectionswithalassetgroupurls: (NSArray<NSURL *> *) Assetgroupurls options: (Nullable phfetchoptions *) options;
There are several ways to get a system album like this no longer described here.
Once we get the album, we need to traverse through each album to get specific photo content, and here are a few common methods.
First introduce the Phfetchresult class, the class instance object can represent a collection of albums, or a specific collection of photos, just like folders, folders can be either a folder or a specific file, so we use it to describe the collection of albums obtained above, It is also used to describe a specific collection of photos.
When we get to the album collection through the above method, we should traverse the Phfetchresult object (similar array, the operation is the same as the array, but the stored data type is the Phassetcollection object). Get a specific album under a collection of albums as follows (the photo collection is the album):
Phfetchresult *result = [Phasset fetchassetsinassetcollection: (Phassetcollection object) Options:nil];
The following method makes a specific album query, in which because the current result is a photo album object, so the picture taken is the album cover photo, Targetsize is the size of the target photo, the frame will provide a photo of your designated size to achieve the purpose of improving performance, If you lack the capital to return the original size of the photo (May occupy a large amount of memory, it is not recommended to do so OH). Phimagerequestoptions is the stretch mode for the photo size. The Phimagerequestoptions parameter is the capture configuration for the photo, and if you need to download the picture from icloud, you can configure it in detail, typically using the default configuration. The UIImage object and the photo information dictionary are provided in the callback block.
Phfetchresult *assetresult =new] resulthandler:^ (UIImage * _nullable result, Nsdictionary * _ Nullable info) { = result; }];
The above is the general picture selector used in the photos framework basic technology, the following is the specific core code.
/*album configuration using the default can be*/phfetchoptions*fetchoptions = [PhfetchoptionsNew]; /*initializing an array of albums*/_devicealumdataarr=[Nsmutablearray array]; /*get system-created albums*/Phfetchresult*smartalbumfetchresult =[phassetcollection fetchassetcollectionswithtype:phassetcollectiontypesmartalbum subtype: Phassetcollectionsubtypeany Options:fetchoptions]; _allablubresult=Smartalbumfetchresult; /*Traverse system Create album result set*/ for(Phassetcollection *subinchSmartalbumfetchresult) {Phfetchresult*result =[Phasset fetchassetsinassetcollection:sub Options:nil]; if(result.count==0) { Continue; } /*add a specific album to an array of albums*/[_devicealumdataarr addobject:sub]; } /*get all user-created albums*/Phfetchresult*SMARTALBUMFETCHRESULT2 =[Phassetcollection Fetchtoplevelusercollectionswithoptions:nil]; /*Traverse User-built album result set*/ for(Phassetcollection *subinchsmartAlbumFetchResult2) {Phfetchresult*result =[Phasset fetchassetsinassetcollection:sub Options:nil]; if(result.count==0) { Continue; } /*add a specific album to an array of albums*/[_devicealumdataarr addobject:sub]; }
As follows: If you have a friend need source code, can go to github download Oh, like the words remember start?? : Https://github.com/China131/JHM_TShow.git
Using the photos frame to build the beauty 美图秀秀 album selector