IOS Uipickercontroller Call camera, select Image upload, with preview function

Source: Internet
Author: User

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

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.