Simple use of iOS to bring your own photo cropping function

Source: Internet
Author: User

Today's project to use the image of the cutting function, originally wanted to find a third party, but looked up the information, I think if just to achieve a simple implementation is not difficult, so decided to write their own

About the selection of pictures//Click to select the view of the picture to pop up two options, take photos and choose Local-(void) addimageclick{[_drawlineview Removefromsuperview];        [Self takephotoaction]; Uialertcontroller *actionsheet = [Uialertcontroller alertcontrollerwithtitle:@ "Please select Get Method" Message:nil PreferredStyle:        Uialertcontrollerstyleactionsheet]; [Actionsheet addaction:[uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel handler:^ (UIAlertAction        *action) {//Cancel button tappped.    [Self Dismissviewcontrolleranimated:yes completion:^{}];        }]]; [Actionsheet addaction:[uialertaction actionwithtitle:@ "Take photos" Style:uialertactionstyledefault handler:^ (            Uialertaction *action) {//Take a new photo [self takephotoaction];        }]]; [Actionsheet addaction:[uialertaction actionwithtitle:@ "Select from album" Style:uialertactionstyledefault handler:^ (            Uialertaction *action) {//read from the photo [self browsealbum];        }]]; [Self Presentviewcontroller:actioNsheet Animated:yes Completion:nil]; }//Photo//Best is to judge the camera is not available, allowsediting is used to set whether the photo can be edited, that is, there will be a box to constrain the proportions of the picture//SourceType used in the photo Uiimagepickercontrollersourcetypecamera with uiimagepickercontrollersourcetypesavedphotosalbum-(void) when selecting a picture takephotoaction {BOOL Iscamera = [Uiimagepickercontroller iscameradeviceavailable:    Uiimagepickercontrollercameradevicerear]; if (!iscamera) {//If not available, pop-up warning box uialertview *alert = [[Uialertview alloc] initwithtitle:@ "No camera available" Message:nil Delegat        E:self cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil];        [Alert show];    Return    } Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc] init];    Imagepicker.sourcetype = Uiimagepickercontrollersourcetypecamera;    imagepicker.allowsediting = YES;    Imagepicker.delegate = self; [Self Presentviewcontroller:imagepicker animated:yes completion:nil];} Whether you take a photo or a local picture, this method is called when you're done//info all the information about the picture in the Uiimagepickercontrolleroriginalimage this key is the original UiimagepickercontrollEreditedimage took the picture after the cropped image-(void) Imagepickercontroller: (Uiimagepickercontroller *) picker Didfinishpickingmediawithinfo: (nsdictionary *) info{nsstring *mediatype = info[@ "Uiimagepickercontrollermediatype"]    ; if ([mediatype isequaltostring:@ "Public.image"]) {//Determine if the picture is UIImage *image = [Info objectforkey:uiimagepickercon         trollereditedimage];//here get the picture//Get the picture, what do you want to do ... {//do something}//By judging the sourcetype of picker, if it is photographed then save to the album Go if (Picker.sourcetype = Uiimagep Ickercontrollersourcetypecamera) {Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didfinishsavingw                              Itherror:contextinfo:), nil); }}}-(void) Image: (UIImage *) image didfinishsavingwitherror: (nserror *) error ContextInfo: (void *) contextinfo{//W    As there an error?        if (Error! = NULL) {//Show error message ... [Wstoast showToastWithTitle:error.description Duration:toast_view_time Completecallback:nil]; } else//No errors {//Show message image successfully saved [wstoast showtoastwithtitle:@ "Picture saved" D    Uration:toast_view_time Completecallback:nil]; }}

Wstoast is my project inside the package of the prompt display box, of course, this is simply the implementation of the crop, the proportion of cutting is fixed, and can not be more select, is simply the need for it

Simple use of iOS to bring your own photo cropping 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.