Transfer the system camera or get the picture from your local photo album.

Source: Internet
Author: User

1. First of all, three agreements must be followed.

Uiactionsheetdelegate,uinavigationcontrollerdelegate,uiimagepickercontrollerdelegate


2. Main code

Select Avatar-(ibaction) Handleselectimage: (ID) Sender {self.myactionsheet = [[Uiactionsheet alloc] Initwit Htitle:nil delegate:self cancelbuttontitle:@ "Cancel" Destructivebutto    Ntitle:nil otherbuttontitles:@ "Open camera" @ "get from album", Nil]; [Self.myactionsheet ShowInView:self.view];} #pragma mark-uiactionsheetdelegate-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (NSInteger    ) buttonindex{if (Buttonindex = = Self.myActionSheet.cancelButtonIndex) {NSLog (@ "you canceled");            } switch (buttonindex) {case 0: [Self takephoto];        Break            Case 1: [Self localphoto];        Break    Default:break;    }}//Photography-(void) takephoto{uiimagepickercontrollersourcetype sourcetype = Uiimagepickercontrollersourcetypecamera; if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {UIIMagepickercontroller *picker = [[Uiimagepickercontroller alloc] init];        Picker.delegate = self;        Set the picture after the photo can be edited picker.allowsediting = YES;        Picker.sourcetype = sourcetype;    [Self Presentviewcontroller:picker animated:yes completion:nil];    }else {NSLog (@) simulates the camera cannot be turned on, please use it in the real machine);    }}//Select Local picture-(void) localphoto{uiimagepickercontroller *picker = [[Uiimagepickercontroller alloc] init];    Picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;    Picker.delegate = self;    picker.allowsediting = YES; [Self Presentviewcontroller:picker animated:yes completion:nil];} When a picture is selected, enter here-(void) Imagepickercontroller: (uiimagepickercontroller*) Picker Didfinishpickingmediawithinfo: (        Nsdictionary *) info{nsstring *type = [info objectforkey:uiimagepickercontrollermediatype]; When the selected type is picture if ([Type isequaltostring:@ "Public.image"]) {//First turn the picture into nsdata uiimage* image = [Info object forkey:@ "UiimagepickercontrollerorigiNalimage "];        NSData *data;        if (uiimagepngrepresentation (image) = = nil) {data = UIImageJPEGRepresentation (image, 1.0);        } else {data = uiimagepngrepresentation (image); }//Picture saved path//Here the picture is placed in the sandbox's Documents folder NSString * Documentspath = [Nshomedirectory () Stringbya                ppendingpathcomponent:@ "Documents"];                File Manager Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; Copy the data object that you just converted into a sandbox and save it as Image.png [FileManager createdirectoryatpath:documentspath withintermediatedirectories:        YES Attributes:nil Error:nil];                [FileManager createfileatpath:[documentspath stringbyappendingstring:@ "/image.png"] contents:data Attributes:nil];                Get the full path of the picture in the sandbox after the selection self.filepath = [[NSString alloc]initwithformat:@ "%@%@", Documentspath, @ "/image.png"];      Close the album interface [Picker Dismissviewcontrolleranimated:yes Completion:nil];          Create a selection after the small icon of the picture placed below//similar to the effect of a meager selection chart uiimageview *smallimage = [[Uiimageview alloc] initWithFrame:                CGRectMake (110, 390, 75, 40)];        Smallimage.image = image;            Add in view [self.imageselectbtn removefromsuperview];//remove button [Self.view addsubview:smallimage]; }    }
<pre name= "code" class= "OBJC" >-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{    NSLog (@ "You have canceled the selection of pictures");    [Picker Dismissviewcontrolleranimated:yes completion:nil];}



Transfer the system camera or get the picture from your local photo album.

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.