iOS8.0 common use of photos.framework for albums

Source: Internet
Author: User
Tags call back

Reprinted from: http://blog.csdn.net/longitachi/article/details/50130957

1. Judging the album access rights

First we visit the album, there must be a need to determine whether there is access to the time, and then make the appropriate action

Phauthorizationstatus status = [Phphotolibrary authorizationstatus]; if (Status = = Phauthorizationstatusrestricted | | = = phauthorizationstatusdenied)    {//  here is no access}     
2. Real-time Monitoring album internal picture changes

[[Phphotolibrary sharedphotolibrary] registerchangeobserver:self];

Of course, this "self" is the callback method to abide by the Phphotolibrarychangeobserver protocol, but this place has a hole, that is, this callback is in a sub-thread, if you want to manipulate the UI, you must call back the main thread
// album Change callback -(void) Photolibrarydidchange: (Phchange *) changeinstance{    ^{         // Your Codes     });}
3. Read all the picture resources in the album related to the album resources, Apple has been placed in the Phasset
#pragmaMark-Get all Photo resources in the album-(Nsarray<phasset *> *) Getallassetinphotoablumwithascending: (BOOL) ascending{Nsmutablearray<phasset *> *assets =[Nsmutablearray array]; Phfetchoptions*option =[[Phfetchoptions alloc] init]; //when ascending is yes, it is arranged in ascending order by the time the photo was created;Option.sortdescriptors = @[[nssortdescriptor Sortdescriptorwithkey:@"CreationDate"Ascending:ascending]]; Phfetchresult*result =[Phasset fetchassetswithmediatype:phassetmediatypeimage options:option]; [Result Enumerateobjectsusingblock:^(ID_nonnull obj, Nsuinteger idx, BOOL *_nonnull stop) {Phasset*asset = (Phasset *) obj; NSLog (@"Photo name%@", [Asset Valueforkey:@"filename"]);    [Assets Addobject:asset];        }]; returnassets;}
4. According to the acquired Phasset object, parse the picture
Phimagerequestoptions *option =[[Phimagerequestoptions alloc] init]; //show thumbnails only, not control quality display    /** Phimagerequestoptionsresizemodenone, Phimagerequestoptionsresizemodefast,//image with size matching (slightly greater than or slightly less than) based on incoming size, quickly loading Phimagerequestoptionsresizemodeexact//accurate loading of images that match the incoming size*/Option.resizemode=Phimagerequestoptionsresizemodefast; Option.networkaccessallowed=YES; //Param:targetsize is the size of the image you want, you can enter it if you want the original size phimagemanagermaximumsize[[Phcachingimagemanager Defaultmanager] Requestimageforasset:asset targetsize:size contentMode: Phimagecontentmodeaspectfit options:option resulthandler:^ (UIImage * _nullable image, Nsdictionary *_nullable Info) {        //parse out the picture}];
5. Get all Smart albums (that is, the default, such as "All Photos" "screenshot", etc.)
Phfetchresult *smartalbums = [Phassetcollection fetchassetcollectionswithtype:phassetcollectiontypesmartalbum Subtype:phassetcollectionsubtypealbumregular Options:nil]; [smartalbums enumerateobjectsusingblock:^ (phassetcollection * _nonnull collection, Nsuinteger idx, BOOL *stop {    NSLog (@ " album name:%@", Collection.localizedtitle);}];

The title of Smart album is English, the following is a corresponding Chinese name

-(NSString *) Transformablumtitle: (NSString *) title{if([title isequaltostring:@"Slo-mo"]) {        return @"Slow Motion"; } Else if([title isequaltostring:@"recently Added"]) {        return @"recently Added"; } Else if([title isequaltostring:@"Favorites"]) {        return @"Most loved"; } Else if([title isequaltostring:@"recently Deleted"]) {        return @"recently deleted"; } Else if([title isequaltostring:@"Videos"]) {        return @"Video"; } Else if([title isequaltostring:@"All Photos"]) {        return @"All photos"; } Else if([title isequaltostring:@"Selfies"]) {        return @"selfie"; } Else if([title isequaltostring:@"Screenshots"]) {        return @"screen shot"; } Else if([title isequaltostring:@"Camera Roll"]) {        return @"Camera Roll"; }    returnNil;}

In the project plist configuration file, add the following key, the value is set to Yes, you can automatically match the phone system language, return album name. No need to make the above judgment

Localized resources can be mixed yes//or right-click plist File Open as->source Code add <key>cfbundleallowmixedlocalizations </key><true/>

6. Get all user-created albums
Phfetchresult *useralbums = [phassetcollection fetchassetcollectionswithtype:phassetcollectiontypealbum subtype: Phassetcollectionsubtypesmartalbumuserlibrary Options:nil]; [Useralbums enumerateobjectsusingblock:^ (Phassetcollection * _nonnull collection, Nsuinteger idx, BOOL * _nonnull stop) {    NSLog (@ "album Name:%@", Collection.localizedtitle);}];
7. Get all the photo objects under each album (Phasset)
#pragma mark-get all the pictures in the specified album-(nsarray<phasset *> *) getassetsinassetcollection: (Phassetcollection *  ) Assetcollection Ascending: (BOOL) ascending{    nsmutablearray<phasset *> *arr = [Nsmutablearray Array];         *result = [self fetchassetsinassetcollection:assetcollection ascending:ascending];    [Result Enumerateobjectsusingblock:^ (id  _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) {        [arr addobject:obj]; // This obj is the Phasset object     }];     return arr;}
8. Verify that the picture is local (if icloud photo storage is turned on, the picture is uploaded to the web on a timed basis and does not exist locally)
Phimagerequestoptions *option = [[Phimagerequestoptions alloc] init];    option.networkaccessallowed = NO;    option.synchronous = YES;        __block BOOL isinlocalablum = YES;        [[Phcachingimagemanager Defaultmanager] Requestimagedataforasset:asset options:option resultHandler:^ (NSData * _ Nullable ImageData, NSString * _nullable Datauti, uiimageorientation orientation, nsdictionary * _nullable info) {        IsI Nlocalablum = ImageData? Yes:no;    }];    return isinlocalablum;

9. Get Livephoto

Phlivephotorequestoptions *option = [[Phlivephotorequestoptions alloc] init];     = phimagerequestoptionsversioncurrent;     = phimagerequestoptionsdeliverymodeopportunistic;     = YES;        [[Phcachingimagemanager Defaultmanager] Requestlivephotoforasset:asset targetsize:phimagemanagermaximumsize Contentmode:phimagecontentmodeaspectfit options:option Resulthandler:^ (Phlivephoto * _Nullable LivePhoto, Nsdictionary * _nullable info) {        if  (completion) completion (Livephoto, info);    }];

10. Get Video

[[Phcachingimagemanager Defaultmanager] Requestplayeritemforvideo:asset options:nil resultHandler:^ (AVPlayerItem * _ Nullable Playeritem, Nsdictionary * _nullable info) {        if  (completion) completion (Playeritem, info);    }];

iOS8.0 common use of photos.framework for albums

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.