A small example of IOS development -- use UIImagePickerController to create a simple camera application and use uiimagepicker to crop

Source: Internet
Author: User

A small example of IOS development -- use UIImagePickerController to create a simple camera application and use uiimagepicker to crop

This blog post is a summary of the learning process for reading IOS Programming Tutorial from abroad. It is difficult because it is an entry. It's mainly about getting started with the UIImagePickerController controller. Why is this controller? It is used to call the camera function of a device. In the future, you may need to test it on a real machine. Because the iPhone simulator cannot support camera functions, running the test will crash.

 

Web: http://www.appcoda.com/ios-programming-camera-iphone-app

 

As a matter of fact, I did it myself according to the course described in this blog post, and I also typed the code again. I soon became familiar with what this UIImagePickerController is.

To help you understand the use of UIImagePickerController, we will build a simple camera application. This example application is very simple: we will have a main window with a large UIImageView showing the selected photo, and two buttons: one for taking a new photo, and the other for selecting a photo from the photo library.

1. First, create a simple project, and then add the relevant code to the storyboard and the corresponding. m file. This concise tutorial does not use automatic layout. I will not talk much about it. I can read the graph:

2. The complete implementation of ViewController. m is as follows:

1 # import "ViewController. h "2 3 @ interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 4 5 @ property (strong, nonatomic) IBOutlet UIImageView * imageView; 6 7 @ end 8 9 @ implementation ViewController10 11-(void) viewDidLoad {12 [super viewDidLoad]; 13 14 // This code automatically determines whether the current device has the camera function, if no, a window is displayed, prompting 15 if (! [UIImagePickerController failed: Failed]) {16 17 UIAlertView * myAlertView = [[UIAlertView alloc] initWithTitle: @ "Error" 18 message: @ "Device has no camera" 19 delegate: nil20 cancelButtonTitle: @ "OK" 21 otherButtonTitles: nil]; 22 23 [myAlertView show]; 24 25} 26} 27-(IBAction) takePhotot :( UIButton *) sender {28 // create UIImagePickerController controller object 29 UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 30 picker. delegate = self; 31 picker. allowsEditing = YES; 32 picker. sourceType = UIImagePickerControllerSourceTypeCamera; 33 34 [self presentViewController: picker animated: YES completion: nil]; 35} 36-(IBAction) selectPhoto :( UIButton *) sender {37 // create UIImagePickerController object 38 UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 39 picker. delegate = self; 40 picker. allowsEditing = YES; 41 picker. sourceType = require; 42 43 [self presentViewController: picker animated: YES completion: nil]; 44} 45 # pragma mark-proxy method 46-(void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary <NSString *, id> *) info {47 UIImage * chosenImage = info [UIImagePickerControllerEditedImage]; 48 self. imageView. image = chosenImage; 49 50 [picker dismissViewControllerAnimated: YES completion: nil]; 51} 52-(void) Updated :( UIImagePickerController *) picker {53 [picker dismissViewControllerAnimated: YES completion: nil]; 54} 55 56 @ end

For this part of the code, nothing else.
Finally, use your real machine to test and use it.

 

Related Article

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.