iOS8 using Alassetslibrar to create a separate folder method and delete a folder flashback bug

Source: Internet
Author: User

Create a new album under iOS8, then manually delete this "Linda Xiao" album in the album, and then use the following code to recreate "Linda Xiao" album is No problem:

 [assetslibrary addassetsgroupalbumwithname:@ "Lin Xiao Xi"                                                   resultblock:^ (Alassetsgroup *group)                   {                      [groups addObject:group];                  }                                                 failureblock:nil]; 

The

ios8+ contains the iOS8 system, which crashes when created with the above code, requires additional photos.framework frames, and imports the header file #import <photos/photos.h> The main part of the code modification is to modify the code above as follows:

  [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^                  {                      [ phassetcollectionchangerequest creationrequestforassetcollectionwithtitle:@ "Lin Xiao Xi"];                  } completionhandler : ^ (bool success, nserror *error)                   {                      if  (!success)  {                           nslog (@ "---error:%@", error.userInfo);                      }                  }];

The following is a complete demo, combined with the online example written, to give you a reference:

1. Add assetslibrary.framework and photos.framework frames

2. Import Header File

#import <AssetsLibrary/AssetsLibrary.h>

#import <Photos/Photos.h>

3. Create a global Uiimageview *_imagev;

-  (void) viewdidload{    [super viewdidload];         _imagev = [[uiimageview alloc] initwithframe:cgrectmake (100, 100,  100, 100)];    _imagev.image = [uiimage imagenamed:@ "Name.png"];     [self.view addSubview:_imageV];    [self  Createalbuminphonealbum];} #pragma  mark -  Create albums in your phone albums-  (void) createalbuminphonealbum{     alassetslibrary *assetslibrary = [[alassetslibrary alloc] init];     NSMutableArray *groups=[[NSMutableArray alloc]init];     alassetslibrarygroupsenumerationresultsblock listgroupblock = ^ (ALAssetsGroup *group,  bool *stop)     {        if  (group)         {            [groups addObject:group];         }        else         {             BOOL haveHDRGroup = NO;                         for  (ALASSETSGROUP&NBSP;*GP  in groups)             {                 nsstring *name = [gp valueforproperty:alassetsgrouppropertyname];                                  if  ([name isequaltostring:@ Lin Xiao Xi])                  {                     haveHDRGroup = YES;                 }             }                         if  (!havehdrgroup)              {                 //Add a new album                  // This method on the ios8.0 system, before the album created by the code, manually delete it, and then use this library to create the album just deleted, this time the method callback is not run fail module, it is the normal process, but the group is brought back Nil//                [ assetslibrary addassetsgroupalbumwithname:@ "Lin Xiao Xi"//                                                  resultblock:^ (Alassetsgroup *group)//                  {//                      nslog (@ "--------%@", group);//                       [groups addObject:group];//                  }//                                                failureBlock:nil];                 //in order to solve the iOS8.0 above the system cannot be created by the code deleted by the same name album                 [[ phphotolibrary sharedphotolibrary] performchanges:^                  {                      [PHAssetCollectionChangeRequest  creationrequestforassetcollectionwithtitle:@ "Lin Xiao Xi"];                                        } completionhandler:^ (Bool success, nserror *error)                   {                      if   (!success)  {                          nslog (@ "---error:%@", error.userInfo);                       }                  }];                                 havehdrgroup = yes;             }        }             };         [assetslibrary enumerategroupswithtypes:alassetsgroupalbum usingblock:listgroupblock  failureblock:nil];        [self savetoalbumwithmetadata:nil  imagedata:uiimagepngrepresentation (_imagev.image)  customalbumname:@ "North Enterprise Star"  completionBlock:^      {         //Here you can create a method to add success           nslog (@ "-------new album created successfully!! ");     }                      failureblock: ^ (nserror *error)      {         // Handling Add Failed method display alert Let it go back to the main thread execution, or the box won't bounce out          dispatch_async ( Dispatch_get_main_queue (), ^{                           //add failure is generally caused by the user not allowing the app to access the album, This way, you can take a look at this situation and Judge              if ([ error.localizeddescription rangeofstring:@ "User denied access"].location !=  nsnotfound | | [error.localizeddescription rangeofstring:@ "User denied access"].location!=nsnotfound) {                                    UIAlertView *alert=[[UIAlertView  Alloc]initwithtitle:errOr.localizeddescription message:error.localizedfailurereason delegate:nil cancelbuttontitle: Nslocalizedstring (@ "OK",  nil)  otherButtonTitles: nil];                                    [alert show];              }         });      }];} -  (void) Savetoalbumwithmetadata: (nsdictionary *) metadata                       imagedata: (NSData  *) imagedata                 customalbumname: (nsstring *) CUSTOMALBUMNAME&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;      completionblock: (void  (^) (void)) completionblock                    failureblock: ( void  (^) (nserror *error)) failureblock{         alassetslibrary *assetslibrary = [[alassetslibrary alloc] init];     void  (^addasset) (alassetslibrary *, nsurl *)  = ^ (alassetslibrary * Assetslibrary, nsurl *asseturl)  {        [assetslibrary  assetforurl:asseturl resultblock:^ (Alasset *asset)  {             [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll  usingblock:^ (alassetsgroup *group, bool *stop)  {                                  if  ([[Group valueforproperty:alassetsgrouppropertyname] isequaltostring:customalbumname])  {                     [group addAsset:asset];                     if  (Completionblock)  {                          completionblock ();                     }                 }            }  failureblock:^ (NSERror *error)  {                 if  (Failureblock)  {                     failureblock (Error);                 }             }];        } failureblock:^ (NSError  *error)  {            if  (Failureblock)  {                 Failureblock (Error);            }         }];    };    [assetsLibrary  Writeimagedatatosavedphotosalbum:imagedata metadata:metadata completionblock:^ (Nsurl *asseturl, nserror *error)  {        if  (Customalbumname)  {             [assetslibrary addassetsgroupalbumwithname: customalbumname resultblock:^ (Alassetsgroup *group)  {                 if  (group)  {                     [assetsLibrary  assetforurl:asseturl resultblock:^ (Alasset *asset)  {                         [group  addAsset:asset];                         if  (Completionblock)  {                              completionblock ();                         }                     } failureblock:^ (NSError * Error)  {                         if  (Failureblock)  {                              failureblock (Error);                         }                     }];                 } else {                     addasset ( Assetslibrary, asseturl);                 }            } failureblock : ^ (nserror *error)  {                 addasset (Assetslibrary, asseturl);             }];        } else {             if  (Completionblock)  {                 completionblock ();             }        }    }];}


iOS8 using Alassetslibrar to create a separate folder method and delete a folder flashback bug

Related Article

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.