IOS gets and crops images from a camera or album

Source: Internet
Author: User

Today, we have encountered a problem of uploading user portraits. We need to read images from the album or camera. The code is simple. Extract the key parts as follows:


 

/Load user image-(void) Detail {UIActionSheet * sheet; // determine whether the camera is supported if ([UIImagePickerController isSourceTypeAvailable: available]) {sheet = [[UIActionSheet alloc] initWithTitle: @ "select image" delegate: self cancelButtonTitle: nil destructiveButtonTitle: @ "cancel" otherButtonTitles: @ "", @ "select from album", nil];} else {sheet = [[UIActionSheet alloc] initWithTitle: @ "select image" delegate: self cancelButtonTitle: nil destructiveButtonTitle: @ "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 = camera; // determines whether the camera is supported. if ([UIImagePickerController isSourceTypeAvailable: Camera]) {switch (buttonIndex) {case 0: return; case 1: // camera sourceType = UIImagePickerControllerSourceTypeCamera; break; case 2: // album sourceType = break; break ;}} else {if (buttonIndex = 0) {return ;} else {sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;} // jump to the camera or album page UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init]; 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 = encrypt (image, COMPRESSED_RATE); UIImage * compressedImage = [UIImage imageWithData: imageData]; [HttpRequestManager uploadImage: compressedImage httpClient: self. httpClient delegate: self];} // load user image-(void) UesrImageClicked {UIActionSheet * sheet; // determine whether cameras are supported if ([UIImagePickerController isSourceTypeAvailable: available]) {sheet = [[UIActionSheet alloc] initWithTitle: @ "select image" delegate: self cancelButtonTitle: nil destructiveButtonTitle: @ "cancel" otherButtonTitles: @ "", @ "select from album", nil];} else {sheet = [[UIActionSheet alloc] initWithTitle: @ "select image" delegate: self cancelButtonTitle: nil destructiveButtonTitle: @ "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 = camera; // determines whether the camera is supported. if ([UIImagePickerController isSourceTypeAvailable: Camera]) {switch (buttonIndex) {case 0: return; case 1: // camera sourceType = UIImagePickerControllerSourceTypeCamera; break; case 2: // album sourceType = break; break ;}} else {if (buttonIndex = 0) {return ;} else {sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;} // jump to the camera or album page UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init]; 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 = encrypt (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 use the callback function to process the returned image.

 


However, the returned image is not a square and is displayed in the user's portrait View, resulting in obvious stretching. I have studied various cropping algorithms for a long time and even want to create a view for processing. I suddenly found that

UIImagePickerControllerOriginalImage

So I tracked it and checked it:


UIKIT_EXTERN NSString * const UIImagePickerControllerMediaType; // an NSString (UIKIT_EXTERN image)

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 that references

 

It's easy to understand. Use UIImagePickerControllerEditedImage to get everything done.


 

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.