IOS Development: Customize albums, save multiple images to a custom album

Source: Internet
Author: User

1. Custom albums (compatible with IOS7 iOS8)

  • - (void) Viewdidload {Search all photo albums in the library
    [Assetslibrary enumerategroupswithtypes:alassetsgroupalbum usingblock:^ (alassetsgroup *group, BOOL *stop)
    {
    Compare the names of the albums
    if ([@ "Nine-da" compare: [group Valueforproperty:alassetsgrouppropertyname]]==nsorderedsame) {
    Target album is found
    _ishavealbum = YES;
    Return
    }
    if (group==nil && _ishavealbum = = NO) {
    Photo albums is over, target album does not exist, thus create it
    __weak alassetslibrary* weakself = assetslibrary;

    if ([[[[Uidevice Currentdevice] systemversion] floatvalue] >= 8.0) {
    IOS 8 Code
    Create new album.
    [[Phphotolibrary Sharedphotolibrary] performchanges:^{
    [Phassetcollectionchangerequest creationrequestforassetcollectionwithtitle:@ "Nine Da"];
    } completionhandler:^ (BOOL success, Nserror *error) {
    if (!success) {
    NSLog (@ "Error creating album:%@", error);
    }
    }];
    }
    else {
    IOS 7.x Code
    Create new Assets Album
    [Assetslibrary addassetsgroupalbumwithname:@ "Nine Da" Resultblock:nil Failureblock:nil];
    }
    Should is the last iteration anyway, but just in case
    _ishavealbum = YES;
    Return
    }
    } Failureblock:nil];}

    The specific explanation I have explained in the previous blog post (http://www.cnblogs.com/cai-rd/p/4432908.html), here is not much to explain, the above code needs to add a variable ishaveablum = NO;

2. Save multiple photos in a custom album

  •  for  (int  i = 0 ; I < 9 ;  I++  Use the delay to deposit once every 0.1 seconds  dispatch_after (Dispatch_time (Dispatch_time_now, int64_t) ( *i * nsec_per_sec)), Dispatch_get_main_queue (), ^{[Self SA Veimage:yourimage toalbum:  @ " rd   withcompletionblock:^{NSL OG ( @ " save s img Success   " ); }];}); 
    }
    /** * Save pictures to Albums * * @param image < #image description#> * @param albumname < #albumName descrip tion#> * @param completionblock < #completionBlock description#>*/-(void) SaveImage: (uiimage*) Image toalbum: (nsstring*) albumname Withcompletionblock: (void(^) (void)) completionblock{alassetslibrary*assetslibrary =[[Alassetslibrary alloc] init]; //write the image data to the Assets Library (camera roll)[Assetslibrary writeimagetosavedphotosalbum:image. Cgimage Orientation: (alassetorientation) image.imageorientation completionblock:^ (nsurl* asseturl, NSError*error) {        //Error Handling        if(error!=Nil) {            //Completionblock (error);            return; }        //add the asset to the custom photo album[self Addasseturl:asseturl toalbum:albumname withcompletionblock:completionblock]; }];}-(void) Addasseturl: (nsurl*) Asseturl toalbum: (nsstring*) albumname Withcompletionblock: (void(^) (void)) completionblock{alassetslibrary*assetslibrary =[[Alassetslibrary alloc] init]; __block BOOL Albumwasfound=NO; //Search all photo albums in the Library[Assetslibrary enumerategroupswithtypes:alassetsgroupalbum usingblock:^ (alassetsgroup *group, BOOL *stop) {        //Compare the names of the albums        if([Albumname Compare: [Group valueforproperty:alassetsgrouppropertyname]]==nsorderedsame) {            //Target album is foundAlbumwasfound =YES; //get a hold of the photo ' s asset instance[Assetslibrary assetforurl:asseturl resultblock:^ (Alasset *Asset) {                 //add photo to the target album[group Addasset:asset]; //Run the completion blockCompletionblock ();                           } Failureblock:nil]; //album is found, bail out of the method            return; }                   if(Group==nil && albumwasfound==NO) {            //photo albums is over, target album does not exist, thus create it__weak alassetslibrary* weakself =assetslibrary; if([[[[Uidevice Currentdevice] systemversion] Floatvalue] >=8.0) {                //IOS 8 Code//Create new album.[[Phphotolibrary Sharedphotolibrary] performchanges:^{[Phassetcollectionchangerequest creationrequestforassetcollectionwithtitle:@"Nine da"]; } Completionhandler:^ (BOOL success, Nserror *error) {                    if(!success) {NSLog (@"Error Creating album:%@", error);            }                }]; }            Else {                //IOS 7.x Code//Create new Assets album[Assetslibrary addassetsgroupalbumwithname:albumname resultblock:^ (Alassetsgroup *Group) {                     //get the photo ' s instance[Weakself assetforurl:asseturl resultblock:^ (Alasset *Asset) {                          //add photo to the newly created album[group Addasset:asset]; //Call the completion blockCompletionblock ();                 } Failureblock:nil];                } Failureblock:nil]; Albumwasfound=YES; }                        //should is the last iteration anyway, but just in case            return; }} Failureblock:nil];}

3, need to introduce the header file

    • #import <AssetsLibrary/ALAsset.h>#import <AssetsLibrary/ALAssetsLibrary.h># Import <AssetsLibrary/ALAssetsGroup.h>#import <AssetsLibrary/ALAssetRepresentation.h> #import <Photos/Photos.h>

ok~ to fix ~ Have a problem can comment ~ together to explore ~

IOS Development: Customize albums, save multiple images to a custom 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.