iOS save pictures to albums you create

Source: Internet
Author: User

QQ group inside see someone asked to save the picture to a custom album method:

Directly on the code:

#import <Foundation/Foundation.h>#import <AssetsLibrary/AssetsLibrary.h>  void (^saveimagecompletion) (nserror* error); @interface alassetslibrary (Customphotoalbum)-(void) SaveImage: (uiimage*) Image toalbum: (nsstring*) Albumname Withcompletionblock: (saveimagecompletion) Completionblock; -(void) Addasseturl: (nsurl*) Asseturl toalbum: (nsstring*) albumname Withcompletionblock: ( saveimagecompletion) Completionblock; @end
#import "alassetslibrary+customphotoalbum.h"@implementationalassetslibrary (customphotoalbum)-(void) SaveImage: (uiimage*) Image toalbum: (nsstring*) albumname Withcompletionblock: (saveimagecompletion) completionblock{//write the image data to the Assets Library (camera roll)[Self 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: (saveimagecompletion) completionblock{__block BOOL albumwasfound=NO; //Search all photo albums in the Library[Self 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[Self assetforurl:asseturl resultblock:^ (Alasset *Asset) {                                                                                            //add photo to the target album[group Addasset:asset]; //Run the completion blockCompletionblock (nil);                                } Failureblock:completionblock]; //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 =Self ; //Create new Assets album[Self 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 (nil);                                                                                                                } Failureblock:completionblock];                                } Failureblock:completionblock]; //should is the last iteration anyway, but just in case                                return;    }} Failureblock:completionblock]; }@end

When used, just call:

-(void) SaveImage: (uiimage*) Image toalbum: (nsstring*) albumname withcompletionblock: (saveimagecompletion) Completionblock, you can ...

iOS save pictures to albums you create

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.