iOS development Save photos to System album (photo Album)

Source: Internet
Author: User
<span id="Label3"></p><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">Save photos to System albums This feature is available in many social apps, so let's talk about how to save images to the system album (photo Album) today.</p></p>Create Uiimageview<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">Created to <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">UIImageView</code> show the photo, we are going to save it <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">UIImage</code> to the system album (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"><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">#define SCREEN [UIScreen mainScreen].bounds.sizeself.image = [UIImage imageNamed:@"iOSDevTip"];UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN.width - 300) / 2, 70, 300, 150)];imageView.image = self.image;[self.view addSubview:imageView];</code></pre></pre>Create UIButton<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important"> create <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 ">uibutton </code> and binds <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 ">actionclick: </code> event: </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"> UIButton *button = [[UIButton alloc] init];button.frame = CGRectMake( 100, 300, SCREEN.width - 200, 40);[button addTarget:self action:@selector(actionClick:) forControlEvents:UIControlEventTouchUpInside];[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];[button setBackgroundColor:[UIColor orangeColor]];[button setTitle:@"SavePhoto" forState:UIControlStateNormal];[self.view addSubview:button];- (void)actionClick:(UIButton *)button{}</code></pre></pre>Save photo to System album (photo Album)<p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">Called in the <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">actionClick:</code> method:</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">UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);</code></pre></pre><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">This time, we want to know if the save is successful, so we need to develop a <code>回调</code> method</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">// 指定回调方法- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if(!error){ NSLog(@"save success"); }else{ NSLog(@"save failed"); }}</code></pre></pre><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">In this method, we know whether the photo was saved Successfully. then, refresh the UI thread as Needed. The first time users use the app, when they click <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">SavePhoto</code> the button, the system will ask Permission:</p></p><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important"><br></p></p><p style="margin:0px 0px 1.2em!important"><p style="margin:0px 0px 1.2em!important">Demo Address: Iosstrongdemo Next article will tell you how to create your own albums and save Photos.</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 Save photos to System album (photo Album)</p></p></span>

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.