iOS development--useful articles for Swift & saving pictures to albums

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size: 18pt;"><span style="font-size: 18pt;">Save pictures to albums</span></span>Recently in-depth learning about swift-related technologies, Although the sea can not do anything good, but feel a lot of harvest, I believe there will always be the same, so summed up a bit, I hope you like!<span style="color: #ff6600;"><span style="color: #ff6600;">the wording in 1.OC</span></span><p><p>In oc, we need to call this method to save the image to the Album:</p></p><pre><pre><code> <span class="cnblogs_code"><span style="color: #008080;">1</span> <span style="color: #0000ff;">void</span> UIImageWriteToSavedPhotosAlbum(UIImage *image, <span style="color: #0000ff;">id</span> completionTarget, SEL completionSelector, <span style="color: #0000ff;">void</span> *contextInfo);</span> </code></pre></pre><p><p>I think everyone has seen this Method's header file, in the header file there is such a passage</p></p><pre><pre><code>  <span class="cnblogs_code"><span style="color: #008080;">1</span> - (<span style="color: #0000ff;">void</span>)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(<span style="color: #0000ff;">void</span> *)contextInfo;</span> </code></pre></pre><p><p>To save a photo to an album, this optional completion method needs to be defined in the format of the following method.<br>so, in oc, We usually copy this method directly and implement this method directly, for example, a chestnut:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) touchesbegan: (nsset *) touches withevent: (uievent *)<span style="color: #0000ff;"><span style="color: #0000ff;">Event</span></span><span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #000000;"><span style="color: #000000;">uiimagewritetosavedphotosalbum (self.iconView.image, self, @selector (image:didfinishsavingwitherror: contextinfo:), nil);</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error Contextinfo: (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>*<span style="color: #000000;"><span style="color: #000000;">) ContextInfo {</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(error) {</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Save Error</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">saved successfully</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p></p></p><p><p>In the above pest, I saved the image in the pre-defined IconView to the album by touching the event with my finger.</p></p><p><p>And how should the same chestnut be implemented in swift?</p></p><span style="color: #ff6600;"><span style="color: #ff6600;">the wording in 2.swift</span></span><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Override</span></span>Func Touchesbegan (touches:set<nsobject>, withevent<span style="color: #0000ff;"><span style="color: #0000ff;">Event</span></span><span style="color: #000000;"><span style="color: #000000;">: Uievent) {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span>Uiimagewritetosavedphotosalbum (iconview.image, self,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">image:didFinishSavingWithError:contextInfo:</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, Nil)</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span>Func Image (image:uiimage, Didfinishsavingwitherror:nserror?)<span style="color: #000000;"><span style="color: #000000;">, Contextinfo:anyobject) {</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">---</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>didfinishsavingwitherror! =<span style="color: #000000;"><span style="color: #000000;">Nil {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Error</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">OK</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>}</pre><p><p></p></p><pre><pre><span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5;">The same chestnuts, implemented in Swift as above, in swift, we jump to the head of the file and it will be found to be like this,</span></pre></pre><pre><pre><span style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 1.5;"><span class="cnblogs_code"> <span style="color: #008080;">1</span> -(<span style="color: #0000ff;">void</span>) image: (UIImage *) image didfinishsavingwitherror: (nserror *) error Contextinfo: ( <span style="color: #0000ff;"> void</span> *) contextinfo;</span><br><br>Or OC in that set, Apple did not help us write the code of Swift under the format should be how to write, so a lot of people on this should be how to use will produce a lot of doubts, in fact, is like above, the parameter one by one corresponds to the function in Swift to write out the way you Can.</span></pre></pre><p><p>Also add a little bit of knowledge:<br>That's the way we can write that.</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">hint: parameter space parameter alias: type</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Func image (image:uiimage, Didfinishsavingwitherror error:nserror?)<span style="color: #000000;"><span style="color: #000000;">, Contextinfo:anyobject) {</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">---</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if didfinishsavingwitherror! = nil {</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>error! =<span style="color: #000000;"><span style="color: #000000;">Nil {</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Error</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>println<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">OK</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>}</pre><p><p></p></p><p><p>A format like this: (parameter parameter alias: type) didfinishsavingwitherror error:nserror?<br>When called externally, the Didfinishsavingwitherror parameter name is Displayed.<br>When used internally, the error parameter alias is displayed, which is convenient for us to use and more like the notation in OC.</p></p><p><p>iOS development--useful articles for Swift & saving pictures to albums</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.