Preface: This post is I read foreign iOS programming tutorial an introductory article of the learning process summary, difficult, because is the introduction. Basically is the introduction Uiimagepickercontroller this controller, then this controller is why? is to invoke the device camera function. You may need to test on a real machine in the back, because the iphone simulator can't support the camera function, and running tests will crash.
Website: Http://www.appcoda.com/ios-programming-camera-iphone-app
In fact, I follow this blog to explain the process, I did it again, and also knocked over the code, and soon became familiar with the Uiimagepickercontroller is what the thing.
1, the first simple to create a project, and then in the storyboard and corresponding. m file to add the relevant code, this concise tutorial does not use automatic layout, not to say, look at the picture literacy:
2, the following is the complete implementation of this VIEWCONTROLLER.M:
1 #import "ViewController.h"2 3 @interfaceViewcontroller () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>4 5@property (Strong, nonatomic) iboutlet Uiimageview *ImageView;6 7 @end8 9 @implementationViewcontrollerTen One- (void) Viewdidload { A [Super Viewdidload]; - - //This code will automatically determine whether the current device has a camera function, if not, will pop-up window prompt the if(![Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) { - -Uialertview *myalertview = [[Uialertview alloc] Initwithtitle:@"Error" -Message@"Device has no camera" + Delegate: Nil -Cancelbuttontitle:@"OK" + Otherbuttontitles:nil]; A at [Myalertview show]; - - } - } --(Ibaction) Takephotot: (UIButton *) Sender { - //To Create a Uiimagepickercontroller controller object inUiimagepickercontroller *picker =[[Uiimagepickercontroller alloc] init]; -Picker.Delegate=Self ; toPicker.allowsediting =YES; +Picker.sourcetype =Uiimagepickercontrollersourcetypecamera; - the [self presentviewcontroller:picker animated:yes completion:nil]; * } $-(Ibaction) Selectphoto: (UIButton *) Sender {Panax Notoginseng //To Create a Uiimagepickercontroller controller object -Uiimagepickercontroller *picker =[[Uiimagepickercontroller alloc] init]; thePicker.Delegate=Self ; +Picker.allowsediting =YES; APicker.sourcetype =uiimagepickercontrollersourcetypephotolibrary; the + [self presentviewcontroller:picker animated:yes completion:nil]; - } $ #pragmaMark-Proxy method $-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary<NSString *,ID> *) info{ -uiimage* Chosenimage =Info[uiimagepickercontrollereditedimage]; -Self.imageView.image =Chosenimage; the - [Picker Dismissviewcontrolleranimated:yes completion:nil];Wuyi } the-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{ - [Picker Dismissviewcontrolleranimated:yes completion:nil]; Wu } - About @end
Just this part of the code, nothing else.
Finally, use your real machine test.
Small instance of iOS development--uiimagepickercontroller