iOS settings avatar (access to System albums, local uploads)

Source: Internet
Author: User

1.

Uiactionsheet *actionsheet = [[Uiactionsheet alloc] Initwithtitle: @ "set Avatar" Delegate:self can celbuttontitle:@ "Cancel" destructivebuttontitle:nil otherbuttontitles:@ "select local image" @ "photo", nil];

[Actionsheet ShowInView:self.view];

2. Implement the corresponding agent event, Agent Uiactionsheetdelegate, the method is as follows

-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex {

Photo Album 0 Photo 1

Switch (buttonindex) {

Case 0:

Read from the album

[Self readimagefromalbum];

Break

Case 1:

Photo

[Self readimagefromcamera];

Break

Default

Break

}

}

3. Realize the function of reading pictures from the album, the code is as follows

Read from the album

-(void) Readimagefromalbum {

Creating objects

Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc] init];

(select type) to select a photo from the album only

Imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;

Specify the proxy, so we want to implement Uiimagepickercontrollerdelegate, Uinavigationcontrollerdeleg ATE 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];

}

4. Achieve photo function

-(void) Readimagefromcamera {

if ([Uiimagepickercontroller ISSOURCETYPEAVAILABLE:UIIMAGEPICKERCONTROLLERSOURC Etypecamera]) {

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];

} else {

Pop-up window responds to click events

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Warning" message:@ "Camera not detected" Delegate:nil Canc Elbuttontitle:nil otherbuttontitles:@ "OK", nil];

[Alert show];

}

}

5. The picture completes the processing to submit, the Proxy method uipickercontrollerdelegate

Processing after the picture is finished

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) image editinginfo: (Nsdictionary *) Editinginfo {

Image is the modified photo

Add a picture to the corresponding view

[_headimageview Setimage:image];

End action

[Self dismissviewcontrolleranimated:yes completion:nil];

}

iOS settings avatar (access to System albums, local uploads)

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.