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
ok~ to fix ~ Have a problem can comment ~ together to explore ~
IOS Development: Customize albums, save multiple images to a custom album