Uiimagepickercontroller---Pictures add---primary use

Source: Internet
Author: User

1. Create Uiimageview

-(void) Creatphotoimageview
{
Self.photoimageview = [[Uiimageview alloc] Initwithframe:cgrectmake (10, 20, 80, 80)];
Self.photoImageView.backgroundColor = [Uicolor blackcolor];
Open user interaction (default off)
self.photoImageView.userInteractionEnabled = YES;
[Self addSubview:self.photoImageView];
}
2. Add a tap gesture on the created Uiimageview

Pat gesture
UITapGestureRecognizer *TAPGR = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (tapgraction:)];
Add gestures
[Self.rootView.photoImageView ADDGESTURERECOGNIZER:TAPGR];

2.1 Gesture Method---Create uiactionsheet---Set up the proxy (follow the proxy protocol)

-(void) Tapgraction: (UITapGestureRecognizer *) sender
{
Uiactionsheet *sheet = [[Uiactionsheet alloc] initwithtitle:@ "Please select" Delegate:self cancelbuttontitle:@ "Cancel" Destructivebuttontitle:nil otherbuttontitles:@ "Choose from album", @ "take photos", nil];
[Sheet ShowInView:self.rootView];
NSLog (@ "%ld", Sheet.cancelbuttonindex);
}

3. The Uiactionsheetdelegate protocol proxy method---the corresponding button add event---to the created Uiimagepickercontroller set agent (following the proxy protocol uinavigationcontrollerdelegate, Uiimagepickercontrollerdelegate)

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

NSLog (@ "%ld", Buttonindex);
if (Buttonindex = = Actionsheet.firstotherbuttonindex) {
Detect if a photo source is available
if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) {

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

Get pictures by proxy method
Imagepickervc.delegate = self;
Edit Set Default no Agent method key to Uiimagepickercontrollereditedimage must be set to Yes
imagepickervc.allowsediting = YES;

Specify IMAGEPICKERVC to get from albums
Imagepickervc.sourcetype = uiimagepickercontrollersourcetypephotolibrary;

Modal One controller
[Self PRESENTVIEWCONTROLLER:IMAGEPICKERVC animated:yes completion:nil];
}
}else if (Buttonindex = = Actionsheet.firstotherbuttonindex + 1) {

if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
Uiimagepickercontroller *IMAGEPICKERVC = [[Uiimagepickercontroller alloc] init];
Imagepickervc.delegate = self;
imagepickervc.allowsediting = YES;

Specify IMAGEPICKERVC to get from the camera
Imagepickervc.sourcetype = Uiimagepickercontrollersourcetypecamera;
[Self PRESENTVIEWCONTROLLER:IMAGEPICKERVC animated:yes completion:nil];
}
}
}

4.uinavigationcontrollerdelegate,uiimagepickercontrollerdelegate protocol Method---Cancel the modal---take out the picture---Set the picture to the corresponding Uiimageview

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info
{

Picker disappears (modal disappears)
[Picker Dismissviewcontrolleranimated:yes Completion:nil];

Take out the picture based on the fixed key value.
UIImage *image = [info objectforkey:uiimagepickercontrollereditedimage];
Self.rootView.photoImageView.image = image;

}

This is done. Add a picture to a Uiimageview from a photo album or selfie

Uiimagepickercontroller---Pictures add---primary use

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.