iOS Learning notes-save pictures to albums

Source: Internet
Author: User

Recent projects to use, this is their own practiced hand program

1 //2 //VIEWCONTROLLER.M3 //Sjzsaveimage4 //5 //Created by Mac on 16/6/3.6 //copyright©2016 Year Mac. All rights reserved.7 //8 9 #import "ViewController.h"Ten  One @interfaceViewcontroller () <UIActionSheetDelegate> A  -@property (nonatomic, strong) UIImage *image; -  the @end -  - @implementationViewcontroller -  +- (void) Viewdidload { - [Super Viewdidload]; +Self.title =@"Save Picture"; A      atUIImage * image = [UIImage imagenamed:@"saveimage.jpg"]; -Self.image =image; -      -Uiimageview * ImageView = [[Uiimageview alloc] Initwithframe:cgrectmake (0,0, Self.view.frame.size.width, Self.view.frame.size.height/2)]; -Imageview.center =Self.view.center; -Imageview.image =image; in      - [Self.view Addsubview:imageview]; to      + [self Createbutton]; - } the  *- (void) Createbutton $ {Panax NotoginsengUIButton * button = [[UIButton alloc] Initwithframe:cgrectmake ( +, Self.view.frame.size.height *7/8, Self.view.frame.size.width- the, +)]; -Button.backgroundcolor =[Uicolor Whitecolor]; the[Button Settitle:@"Save Picture"Forstate:uicontrolstatenormal]; + [button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; AButton.layer.cornerRadius =4.0; the [button addtarget:self action: @selector (ButtonClick) forcontrolevents:uicontroleventtouchupinside]; + [Self.view Addsubview:button]; - } $  $- (void) ButtonClick - { -     if([[Uidevice currentdevice].systemversion Doublevalue] >8.0){ the         //after iOS8, replace Uiactionsheet with Uialertcontroller . -Uialertcontroller * Alert =[Uialertcontroller alertcontrollerwithtitle:nil message:nil Preferredstyle:uialertcontrollerstyleactionsheet] ;Wuyi          the         //Add Save Picture Action -Uialertaction *action = [uialertaction actionwithtitle:@"Save Picture"Style:uialertactionstyledefault handler:^ (Uialertaction *action) { Wu              -             //To save a picture to an album, the callback function must be in this form About Uiimagewritetosavedphotosalbum (Self.image, Self, @selector (image:didFinishSavingWithError:contextInfo:), N ULL); $              -         }]; -          -[Action Setvalue:[uicolor Blackcolor] Forkey:@"_titletextcolor"]; A [alert addaction:action]; +          the         //Add Cancel Action -Uialertaction * Actioncancel = [uialertaction actionwithtitle:@"Cancel"Style:uialertactionstylecancel handler:^ (Uialertaction *_nonnull Action) { $              the         }]; the[Actioncancel Setvalue:[uicolor Redcolor] Forkey:@"_titletextcolor"]; the [alert Addaction:actioncancel]; the          - [self Presentviewcontroller:alert animated:yes completion:nil]; in}Else{ theUiactionsheet * action = [[Uiactionsheet alloc] Initwithtitle:nilDelegate: Self Cancelbuttontitle:@"Cancel"Destructivebuttontitle:@"Save Picture"Otherbuttontitles:nil, nil]; the [Action ShowInView:self.view]; About     } the } the  the- (void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (nsinteger) Buttonindex + { -     if(Buttonindex = =0){ the         //To Save the picture to the album, the callback for the function must be in this formBayi Uiimagewritetosavedphotosalbum (Self.image, Self, @selector (image:didFinishSavingWithError:contextInfo:), NULL ); the     } the } -  - //Save Picture callback function the- (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) ContextInfo the { the     //Here we can set the popup box the     if(Error) { -NSLog (@"Save failed"); the}Else{ theNSLog (@"saved successfully"); the     }94 } the  the @end

iOS Learning notes-save pictures to albums

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.