IOS get pictures and crop from camera or album

Source: Internet
Author: User

Today encountered a user picture upload problem, you need to read from the photo album or camera. The code is simple and extracts the key parts as follows:

Load user image-(void) uesrimageclicked{uiactionsheet *sheet; Determine if the camera is supported if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {She ET = [[Uiactionsheet alloc] initwithtitle:@ "Select Image" Delegate:self cancelbuttontitle:nil destructivebuttontitle:@ "Cancel"    otherbuttontitles:@ "Photo", @ "select from album", Nil]; } else {sheet = [[Uiactionsheet alloc] initwithtitle:@ "Select Image" Delegate:self cancelbuttontitle:nil destructivebut    tontitle:@ "Cancel" otherbuttontitles:@ "Select from album", Nil];        } Sheet.tag = 255; [Sheet ShowInView:self.view];} #pragma mark-action sheet delegte-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) buttonindex{if (Actionsheet.tag = = 255) {Nsuinteger sourcetype = uiimagepickercontrollersourcetypephotolibrary        ;            Determine if the camera is supported if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {             Switch (buttonindex) {   Case 0:return;                    Case 1://Camera sourcetype = Uiimagepickercontrollersourcetypecamera;                Break                    Case 2://album sourcetype = Uiimagepickercontrollersourcetypephotolibrary;            Break            }} else {if (Buttonindex = = 0) {return;            } else {sourcetype = Uiimagepickercontrollersourcetypesavedphotosalbum; }}//Jump to camera or album page Uiimagepickercontroller *imagepickercontroller = [[Uiimagepickercontroller alloc] I        NIT];        Imagepickercontroller.delegate = self;        imagepickercontroller.allowsediting = YES;                Imagepickercontroller.sourcetype = sourcetype;    [Self Presentviewcontroller:imagepickercontroller Animated:yes completion:^{}]; }} #pragma mark-image picker delegte-(void) Imagepickercontroller: (Uiimagepickercontroller *) picker DidfinishpickingmediaWithinfo: (nsdictionary *) Info{[picker Dismissviewcontrolleranimated:yes completion:^{}];        UIImage *image = [info uiimagepickercontrolleroriginalimage];        Userimageview.image = image;    NSData *imagedata = uiimagejpegrepresentation (image, Compressed_rate);        UIImage *compressedimage = [UIImage imagewithdata:imagedata];    [Httprequestmanager uploadimage:compressedimage httpClient:self.httpClient delegate:self]; }


Create a form, select a camera or album, and then use the callback function to process the returned picture.


However, the returned image is not a square, showing a noticeable stretch in the view of the user's avatar. Studied for a long time various cutting algorithms, and even want to create a view to deal with. Suddenly found in the code from the camera, album to take pictures of the

Uiimagepickercontrolleroriginalimage

So I followed it in a look:

Uikit_extern NSString *const uiimagepickercontrollermediatype; //An nsstring (UTI, i.e. kuttypeimage)

Uikit_extern NSString *const uiimagepickercontrolleroriginalimage; //A UIImage

Uikit_extern NSString *const uiimagepickercontrollereditedimage; //A UIImage

Uikit_extern NSString *const uiimagepickercontrollercroprect; //An Nsvalue (cgrect)

Uikit_extern NSString *const uiimagepickercontrollermediaurl; //An nsurl

Uikit_extern NSString *const uiimagepickercontrollerreferenceurl ns_available_ios (4_1); //An nsurl-references an

Suddenly, very simple, with uiimagepickercontrollereditedimage, everything is done.

IOS get pictures and crop from camera or 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.