<span id="Label3"></p><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">In just one article online on iOS development Save photos to System album (photo Album), We talked about how to keep photo system Albums. Is there any other way to save it?</p></p><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">Saving photos is also available <code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline">ALAssetsLibrary</code> , <code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline">ALAssetsLibrary</code> providing access to photos and videos on iOS devices and a bridge between connecting apps and Albums.</p></p><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">next, Let's take a detailed look at the system album permissions to get, save photos, Create your own albums and so On.</p></p>Create your own albums<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">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:</p></p><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline; white-space:pre; overflow:auto; border:1px solid rgb(204,204,204); padding:0.5em 0.7em; display:block!important"> ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) { //创建相簿成功} failureBlock:^(NSError *error) { //失败}];</code></pre></pre>Save photo<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">This method also saves the photo to the system album, not to the album you Created. The code is as Follows:</p></p><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline; white-space:pre; overflow:auto; border:1px solid rgb(204,204,204); padding:0.5em 0.7em; display:block!important"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline; white-space:pre; overflow:auto; border:1px solid rgb(204,204,204); padding:0.5em 0.7em; display:block!important"> 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]; }}];</code></code></pre>Get Permissions<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">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:</p></p> <ol style="margin:1.2em 0px; padding-left:2em"> <ol style="margin:1.2em 0px; padding-left:2em"> <li style="margin:0.5em 0px"><p style="margin:0px 0px 1.2em!important; margin:0.5em 0px!important">Failed to create photo album</p></li> <li style="margin:0.5em 0px"><p style="margin:0px 0px 1.2em!important; margin:0.5em 0px!important">Failed to save photo</p></li> </ol> </ol><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">In the previous two ways to <strong>create your own albums</strong> and <strong>save photos</strong> 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:</p></p><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline; white-space:pre; overflow:auto; border:1px solid rgb(204,204,204); padding:0.5em 0.7em; display:block!important"><code style="font-size:0.85em; font-family:consolas,inconsolata,courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px Solid RGB (234,234,234); Background-color:rgb (248,248,248); display:inline; white-space:pre; overflow:auto; border:1px Solid RGB (204,204,204); Padding:0.5em 0.7em; Display:block!important ">alassetslibrary *library = [[alassetslibrary alloc] init]; [library addassetsgroupalbumwithname:@ "test" resultblock:^ (alassetsgroup *group) {//create album succeeded} failureblock:^ (nserror *error) {uialertview *alert = [[uialertview alloc]initwithtitle:@ "storage failed" message:@ "please Open Settings-privacy-photos to set up" Delegate:nil cancelbuttontitle:@ "ok" otherbuttontitles:nil, nil]; [alert show];}]; </code></code></pre><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">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.</p></p>Save photos to your albums<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">The following code is found in the big google, personally tested is can be used, organized as Follows:</p></p><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline; white-space:pre; overflow:auto; border:1px solid rgb(204,204,204); padding:0.5em 0.7em; display:block!important">#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 (); } } }];}</code></pre>Alassetslibrary+customphotoalbum Saving Photos<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline">github</code>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:</p></p><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline; white-space:pre; overflow:auto; border:1px solid rgb(204,204,204); padding:0.5em 0.7em; display:block!important">#import "ALAssetsLibrary+CustomPhotoAlbum.h"</code></pre></pre><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">Save the photo to your own album and call it Directly:</p></p><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><pre style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; font-size:1em; line-height:1.2em; margin:1.2em 0px"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline; white-space:pre; overflow:auto; border:1px solid rgb(204,204,204); padding:0.5em 0.7em; display:block!important">ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];[library saveImage:self.image toAlbum:@"gang" completion:^(NSURL *assetURL, NSError *error) { if (!error) { }} failure:^(NSError *error) {}];</code></pre></pre><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important"><code style="font-size:0.85em; font-family:Consolas,Inconsolata,Courier,monospace; margin:0px 0.15em; padding:0px 0.3em; white-space:pre-wrap; border:1px solid rgb(234,234,234); background-color:rgb(248,248,248); display:inline">ALAssetsLibrary+CustomPhotoAlbum</code>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.</p></p>?<p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>iOS development saves photos to albums you create</p></p></span>
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