"Learn iOS: UI Series" Click the change avatar to read photos and take pictures from the album two functions

Source: Internet
Author: User

The functions are as follows:

1. Click on the avatar to select the change Avatar Mode ① album ② Photography.

2. Click on the album to achieve the replacement by reading the system album and getting the picture.

3. Click on the camera to take pictures and replace the photos.

4. If the camera, pop-up box warning.

The code is as follows:

1. Prompt function via Uiactionsheet object

   Create Object    Uiactionsheet *actionsheet = [[Uiactionsheet alloc] Initwithtitle:                             @ "hint" delegate:self cancelbuttontitle:@ "Cancel"                     Destructivebuttontitle:nil otherbuttontitles:@ "album", @ "photo", nil];    Show     [Actionsheet ShowInView:self.view] on the view;    [Actionsheet release];
2. Implement the corresponding agent event, Agent Uiactionsheetdelegate, the method is as follows

-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex:                                                     (Nsinteger) Buttonindex {    //album 0 photo 1    switch (buttonindex) {case        0:            //Read from the album            [self readimagefromalbum];            break;        Case 1:            //Take photos            [self readimagefromcamera];            break;        Default: Break            ;    }}
3. Realize the function of reading pictures from the album, the code is as follows

Read from album-(void) Readimagefromalbum {    //Create object    uiimagepickercontroller *imagepicker = [[ Uiimagepickercontroller alloc] init];    (select type) indicates that only photos are selected from the album    imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;    Specify the proxy, so we want to implement Uiimagepickercontrollerdelegate,                                               uinavigationcontrollerdelegate protocol    imagepicker.delegate = Self;    Sets whether to skip to edit mode for picture clipping after the album has finished selecting photos. (Allow user to edit)    imagepicker.allowsediting = YES;    Show albums    [self Presentviewcontroller:imagepicker animated:yes completion:nil];    [Imagepicker release];    }
4. Achieve photo function

-(void) Readimagefromcamera {        if ([Uiimagepickercontroller issourcetypeavailable:                                         Uiimagepickercontrollersourcetypecamera]) {       Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller ALLOC] init];        Imagepicker.sourcetype = Uiimagepickercontrollersourcetypecamera;        Imagepicker.delegate = self;        imagepicker.allowsediting = YES; Allow user to edit        [self presentviewcontroller:imagepicker animated:yes completion:nil];        [Imagepicker release];    } else {        //Popup Response Click event        Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Warning"                       message:@ "Camera not detected" Delegate:nil cancelbuttontitle:nil                                               otherbuttontitles:@ "OK", nil];        [Alert show];        [Alert release];}    }
5. The picture completes the processing to submit, the Proxy method uipickercontrollerdelegate

After the picture is finished processing-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker      didfinishpickingimage: (UIImage *) Image editinginfo: (nsdictionary *) Editinginfo {       //image is the modified photo    //Add the picture to the corresponding view    [button setimage:image Forstate:uicontrolstatenormal];    End Operation    [self Dismissviewcontrolleranimated:yes completion:nil];}



"Learn iOS: UI Series" Click the change avatar to read photos and take pictures from the album two functions

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.