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