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