Add delegate to. h
@interface ViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
Implementing button Events
-(Ibaction) Chooseimage: (ID) Sender { Uiactionsheet *sheet;<p>//determines if camera </p>if is supported ([ Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) { sheet = [[ Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" otherbuttontitles:@ "Photo", @ "select from album", Nil]; } else { sheet = [[Uiactionsheet alloc] initwithtitle:@ "select" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel" otherbuttontitles:@ "Select from album", Nil]; } Sheet.tag = 255; [Sheet ShowInView:self.view]; }
Implementing the Actionsheet Delegate event
Determine if the camera is supported and jump to the camera or album interface
-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) buttonindex{if (ActionSheet.tag = = 255) {Nsuinteger sourcetype = 0; Determine if the camera is supported if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) { Switch (buttonindex) {case 0://Cancel return; Case 1://Camera sourcetype = Uiimagepickercontrollersourcetypecamera; Break Case 2://album sourcetype = Uiimagepickercontrollersourcetypephotolibrary; Break }} else {if (Buttonindex = = 0) {return; } else {sourcetype = Uiimagepickercontrollersourcetypesavedphotosalbum; }<p>//jump to the camera or album page </P>uiimagepickercontroller *imagepickercontroller = [[Uiimagepickercontroller alloc] init]; Imagepickercontroller.delegate = self; imagepickercontroller.allowsediting = YES; Imagepickercontroller.sourcetype = sourcetype; [Self Presentviewcontroller:imagepickercontroller Animated:yes completion:^{}]; [Imagepickercontroller release]; }}
Implement Imagepicker] Delegate event
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info {[Picker Dismissviewcontrolleranimated:yes completion:^{}]; UIImage *image = [info objectforkey:uiimagepickercontrolleroriginalimage]; /* Here info has six values * UIIMAGEPICKERCONTROLLERMEDIATYPE; An nsstring uttypeimage) * uiimagepickercontrolleroriginalimage; A UIImage original picture * uiimagepickercontrollereditedimage; A UIImage picture after cropping * uiimagepickercontrollercroprect; An nsvalue (cgrect) * UIIMAGEPICKERCONTROLLERMEDIAURL; An nsurl * Uiimagepickercontrollerreferenceurl//A nsurl that references a asset in the Assetslibrary Fram Ework * Uiimagepickercontrollermediametadata//Nsdictionary containing metadata from a captured photo * * Save the image locally, see below [Self saveimage:image withname:@ "currentimage.png"]; NSString *fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]stringbyappendingpathcomponent:@ "Currentimage.png"]; UIImage *savedimage = [[UIImage alloc] initwithcontentsoffile:fullpath]; IsFullScreen = NO; [Self.imageview Setimage:savedimage]; Self.imageView.tag = 100; }-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{[self dismissviewcontrolleranimated:yes completion:^{}];}
Save Picture Method
#pragma mark-Save picture to Sandbox-(void) SaveImage: (UIImage *) currentimage withname: (NSString *) imagename{ NSData *imagedata = UIImageJPEGRepresentation (Currentimage, 0.5); Get the Sandbox directory nsstring *fullpath = [[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] Stringbyappendingpathcomponent:imagename]; Write the picture to the file [ImageData writetofile:fullpath atomically:no];}
Achieve click picture Preview function, Slide zoom zoom out, drive the picture
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{isfullscreen =!isfullscreen; Uitouch *touch = [touches anyobject]; Cgpoint touchPoint = [Touch LocationInView:self.view]; Cgpoint imagepoint = Self.imageView.frame.origin; Touchpoint.x, Touchpoint.y is the contact coordinates//contacts in the ImageView, click on the ImageView when zoomed in, click again to zoom out if (imagepoint.x <= touchPoint. X && imagepoint.x +self.imageview.frame.size.width >=touchpoint.x && imagepoint.y <= Touchpoint.y && imagepoint.y+self.imageview.frame.size.height >= touchpoint.y) {//Set picture to enlarge animation [ UIView Beginanimations:nil Context:nil]; Animation time [UIView Setanimationduration:1]; if (IsFullScreen) {//enlarge size Self.imageView.frame = CGRectMake (0, 0, 320, 480); } else {//shrink size Self.imageView.frame = CGRectMake (50, 65, 90, 115); }//Commit animation [UiviEW Commitanimations]; } }
IOS Uipickercontroller Call camera, select Image upload, with preview function