About customizing albums Deleting the Resurrection implementation
Here (http://www.cnblogs.com/iCocos/p/4705585.html) We mentioned it.
- 1: Simple implementation of get system album pictures and save Pictures to the system album
- 2: Define a custom album and save it to a custom album
This is a long time to implement all of the above features in a simple example, and add a useful feature to implement
- The custom album in the app is deleted and the photo is saved again after deletion cannot be successful
Here is a library of the system:alassetslibrary Let's take a look at the photos we got in the album.
1- (void) Getallphotos2 {3Alassetslibrary *library =[[Alassetslibrary alloc] init];4 //Traverse all folders, a Alassetsgroup object represents a folder5[Library Enumerategroupswithtypes:alassetsgroupall usingblock:^ (alassetsgroup *group, BOOL *stop) {6 //Traverse all multimedia files (Pictures, videos) within a folder, and a Alasset object represents a picture7[Group enumerateassetsusingblock:^ (Alasset *result, Nsuinteger index, BOOL *stop) {8 //thumbnail Images9Xmglog (@"%@", [UIImage imageWithCGImage:result.thumbnail]);Ten //get the original picture One //Xmglog (@ "%@", [UIImage imageWithCGImage:result.defaultRepresentation.fullResolutionImage]); A }]; - - } Failureblock:nil]; the}
Let's look at the specific implementation of the Code One: First define a property surge to record and implement some other functions
/**/*library;
Second: Then lazy load this property
1 -(alassetslibrary *) library2{3 if (! _library) {4 _library = [[Alassetslibrary alloc] init]; 5 }6 return _library; 7 }8
Three: Click the Save button implementation
1-(ibaction) Save2 {3 //get the name of the folder4__block NSString *groupname =[self groupName];5 6 //Weak reference to self7 xmgweakself;8 9 //Photo GalleryTen__weak alassetslibrary *weaklibrary =self.library; One A //Create a folder -[Weaklibrary addassetsgroupalbumwithname:groupname resultblock:^ (Alassetsgroup *Group) { - if(group) {//the newly created folder the //add a picture to a folder - [Weakself Addimagetogroup:group]; -}Else{//folder already exists -[Weaklibrary enumerategroupswithtypes:alassetsgroupall usingblock:^ (alassetsgroup *group, BOOL *stop) { +NSString *name =[group Valueforproperty:alassetsgrouppropertyname]; - if([name Isequaltostring:groupname]) {//is the folder that you created yourself + //add a picture to a folder A [Weakself Addimagetogroup:group]; at -*stop = YES;//Stop Traversal -}Else if([Name isequaltostring:@"Camera Roll"]) { - //the folder was forcibly deleted by the user -GroupName = [GroupName stringbyappendingstring:@" "]; - //Store a new name in [[Nsuserdefaults standarduserdefaults] Setobject:groupname Forkey:xmggroupnamekey]; - [[Nsuserdefaults standarduserdefaults] synchronize]; to //Create a new folder +[Weaklibrary addassetsgroupalbumwithname:groupname resultblock:^ (Alassetsgroup *Group) { - //add a picture to a folder the [Weakself Addimagetogroup:group]; * } Failureblock:nil]; $ }Panax Notoginseng } Failureblock:nil]; - } the } Failureblock:nil]; + } A
Four: Add a picture
1 /**2 * Add a picture to a folder3 */4- (void) Addimagetogroup: (Alassetsgroup *) Group5 {6__weak alassetslibrary *weaklibrary =self.library;7 //pictures you need to save8Cgimageref image =Self.imageView.image.CGImage;9 Ten //add picture to "Camera Roll" One[Weaklibrary writeimagetosavedphotosalbum:image metadata:nil completionblock:^ (nsurl *assetURL, NSError *error) { A[Weaklibrary assetforurl:asseturl resultblock:^ (Alasset *Asset) { - //add a picture to a custom folder - [group Addasset:asset]; the[Svprogresshud Showsuccesswithstatus:@"Save success!"]; - } Failureblock:nil]; - }]; -}
Five: About the group name in ShaheFirst define a key to save the name and a name to save.
Static Const @" Icocosgroupnamekey " ; Static Const @" Icocos ";
implementing the access of the ancestral name
1-(NSString *) GroupName2 {3 //first get the name from the sandbox4NSString *groupname =[[Nsuserdefaults standarduserdefaults] stringforkey:xmggroupnamekey];5 if(GroupName = = nil) {//No folder names are stored in the sandbox6GroupName =Xmgdefaultgroupname;7 8 //Store name in sandbox9 [[Nsuserdefaults standarduserdefaults] Setobject:groupname Forkey:xmggroupnamekey];Ten [[Nsuserdefaults standarduserdefaults] synchronize]; One } A - returnGroupName; - } the
iOS development--Practical technology OC Articles & about customizing albums Deleting the Resurrection implementation