iOS development saves photos to albums you create
Saving photos is also available ALAssetsLibrary
, ALAssetsLibrary
providing access to photos and videos on iOS devices and a bridge between connecting apps and albums.
Next, let's take a detailed look at the system album permissions to get, save photos, create your own albums and so on.
Create your own albums
It's also a way to create your own albums, and then save your photos or videos to your own albums. The relevant code is as follows:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) { //创建相簿成功} failureBlock:^(NSError *error) { //失败}];
Save photo
This method also saves the photo to the system album, not to the album you created. The code is as follows:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *asSetUrl,NSError *error){ if (error) { //失败 }else{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存储成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; }}];
Get Permissions
If the user closes the album permissions before the photo is saved, this time the save fails. If you do not do any processing, users will not know that they have failed to save. So, we can make the appropriate prompt before saving the photo. How do I get this permission? There are generally two methods:
Failed to create photo album
Failed to save photo
In the previous two ways to create your own albums and save photos of the failure results, we can pop up to get photos permission failed prompt. Let's take the first example of a failure to create a photo album:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) { //创建相簿成功} failureBlock:^(NSError *error) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存储失败" message:@"请打开 设置-隐私-照片 来进行设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show];}];
In the result of failure to save the photo, we can also pop up the corresponding prompt, let the user open the app's album permissions.
Save photos to your albums
The following code is found in the big Google, personally tested is can be used, organized as follows:
#pragma mark-Create album-(void) createalbum{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 *GP in groups) {NSString *name =[gp Valueforproperty:alassetsgrouppropertyna Me]; if ([Name isequaltostring:@ "Iosdevtip"]) {havehdrgroup = YES; }} if (!havehdrgroup) {//do Add a group named "XXXX" [ Etslibrary addassetsgroupalbumwithname:@ "Iosdevtip" resultblock:^ (ALASSETSGR OUP *group) {[Groups Addobject:group];} Failureblock:nil]; Havehdrgroup = YES; } } }; Create albums [Assetslibrary Enumerategroupswithtypes:alassetsgroupalbum Usingblock:listgroupblock FailureBlock:nil]; [Self Savetoalbumwithmetadata:nil imagedata:uiimagepngrepresentation (self.image) customalbumname:@ "IOSDevTip" completionblock:^ {//Here can create a method to add success} failureblock:^ (Nserror *error) {// 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 not allowed to be used by users Ask the album caused, this way can be taken out of this situation to judge if ([Error.localizeddescription rangeofstring:@ "User denied access"].location! = NSNOTF Ound | | [Error.localizeddescription rangeofstring:@ "user denied access"].location!=nsnotfound) {Uialertview *ALERT=[[UIALERTVI EW 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 Completionblock: (void (^) (void)) Completionblock Failureblock: (void (^) (nserror *error)) failureblock{alassetslibrary *assetslibrary = [[Alassetslibrary alloc] Init ]; __weak alassetslibrary *weakself = assetslibrary; void (^addasset) (Alassetslibrary *, nsurl *) = ^ (Alassetslibrary *assetslibrary, Nsurl *asseturl) {[assetslibrary Assetforurl:asseturl resultblock:^ (Alasset *asset) {[Assetslibrary enumerategroupswithtypes:alassetsgroupall u singblock:^ (Alassetsgroup *group, BOOL *stop) {if ([[Group Valueforproperty:alassetsgrouppropertyname] IsE Qualtostring:customalbumname]) {[Group Addasset:asset]; if (Completionblock) { Completionblock (); }}} failureblock:^ (Nserror *error) {if (Failureblock) {FAI Lureblock (Error); } }]; } failureblock:^ (Nserror *error) {if (Failureblock) {failureblock (error); } }]; }; [Assetslibrary writeimagedatatosavedphotosalbum:imagedata metadata:metadata completionblock:^ (NSURL *assetURL, Nserror *error) {if (customalbumname) {[Assetslibrary addassetsgroupalbumwithname:customalbumname ResU ltblock:^ (Alassetsgroup *group) {if (group) {[Weakself assetforurl:asseturl Resultbloc k:^ (Alasset *asset) {[Group Addasset:asset]; if (completionblock) {completionblock (); }} failureblock:^ (Nserror *error) {if (FAILureblock) {Failureblock (error); } }]; } else {Addasset (weakself, Asseturl); }} failureblock:^ (Nserror *error) {addasset (weakself, Asseturl); }]; } else {if (completionblock) {completionblock (); } } }];}
Alassetslibrary+customphotoalbum Saving Photos
github
There is a project on the alassetslibrary+customphotoalbum that says save photos made a good package. Before you use, remember to import the header file first:
#import "ALAssetsLibrary+CustomPhotoAlbum.h"
Save the photo to your own album and call it directly:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];[library saveImage:self.image toAlbum:@"gang" completion:^(NSURL *assetURL, NSError *error) { if (!error) { }} failure:^(NSError *error) {}];
ALAssetsLibrary+CustomPhotoAlbum
The encapsulation of the saved video is also very good. I am using this third party to save photos and videos in my project. Because it is good, so recommended for everyone to use.
The source of this article just online: http://www.superqq.com/blog/2015/08/04/save-photo-to-own-album/
iOS development saves photos to albums you create