iOS development calls system camera and turns on flash
Today to share with you how to call the iphone's camera function and turn on the flash, some code I do not understand, many are on the internet to learn from others. iOS has two ways to take photos and videos: 1. Using Uiimagepickercontroller directly, this class provides a simple and convenient way to take photos and select pictures in the Image library. 2. The other is to fully customize the picture-taking interface and select the image Library interface through the Avfoundation.framework framework. I only did the first one, I will first introduce you to the first way:
First, before calling the interface, we need to first determine whether the current device supports Uiimagepickercontroller, using issourcetypeavailable: To determine whether it is available
Second, look at the type of media that matches, this time we call Availablemediatypeforsourcetype: Judge
We need to add his two proxy methods when calling Uiimagepickercontroller:
Uinavigationcontrollerdelegate and Uiimagepickercontrollerdelegate, you can also adjust the flash when you call the camera, there is in the code.
To invoke the flash you need to first build an instance object of the Avcapturesession class:
[Java]View Plaincopy
- #import <uikit/uikit.h>
- //call the Flash call frame
- #IMPORT&NBSP;<AVFOUNDATION/AVFOUNDATION.H>&NBSP;&NBSP;
-
- @interface CameraViewController : UIViewController< uinavigationcontrollerdelegate, uiimagepickercontrollerdelegate>
- {
- avcapturesession * _avsession;
- }
- &NBSP;&NBSP;&NBSP;
- @property (nonatomic,retain) avcapturesession * avsession;
Li class= "alt" >&NBSP;&NBSP;&NBSP;
- @end
In. m-(void) Viewdidload build 4button,camera call camera, library call Photo Gallery, flashlight Open flash, close close flash
[Java]View Plaincopy
- Turn on the camera
- -(void) Addcarema
- {
- //Determine if camera can be turned on, simulator this function is not available
- if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
- Uiimagepickercontroller * Picker = [[Uiimagepickercontroller alloc]init];
- Picker.delegate = self;
- picker.allowsediting = YES; //whether editable
- //Camera
- Picker.sourcetype = Uiimagepickercontrollersourcetypecamera;
- [Self presentmodalviewcontroller:picker animated:yes];
- [Picker release];
- }else{
- //If the user is not prompted
- Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Error" message:@"You don't have a webcam" delegate:nil cancelbuttontitle:@"drat!" otherbuttontitles:nil];
- [Alert show];
- }
- }
After you open the camera, then you need to call the method in Uiimagepickercontrollerdelegate, the method that is executed after the shot is completed, and the method that executes after clicking Cancel:
[Java]View Plaincopy
- The method to be executed after the shooting is completed
- -(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) Info
- {
- //Get Pictures
- UIImage * image = [info objectforkey:uiimagepickercontrolleroriginalimage];
- //images in albums
- Uiimagewritetosavedphotosalbum (image, nil, nil, nil);
- [Self dismissmodalviewcontrolleranimated:yes];
- }
- To execute a method after clicking the Cancel button
- -(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) Picker
- {
- [Self dismissmodalviewcontrolleranimated:yes];
- }
Call camera photo and save to Picture Library has completed.
Next, open photo Gallery:
[Java]View Plaincopy
- -(void) openpiclibrary
- {
- //albums can be opened with an emulator
- if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) {
- Uiimagepickercontroller * Picker = [[Uiimagepickercontroller alloc]init];
- Picker.delegate = self;
- picker.allowsediting = YES; //Whether you can edit
- //Open album Select Photos
- Picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
- [Self presentmodalviewcontroller:picker animated:yes];
- [Picker release];
- }else{
- Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Error" message:@"You don't have a webcam" delegate:nil cancelbuttontitle:@"drat!" otherbuttontitles:nil];
- [Alert show];
- }
- }
- Select the proxy method for the image to enter
- -(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) image Editinginfo: (nsdictionary *) Editinginfo
- {
- [Self dismissmodalviewcontrolleranimated:yes];
- }
Code to call the Flash
[Java]View Plaincopy
- -(void) Openflashlight
- {
- Avcapturedevice * device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
- if (Device.torchmode = = Avcapturetorchmodeoff) {
- //create an AV session
- Avcapturesession * session = [[Avcapturesession alloc]init];
- //Create device input and add to current session
- Avcapturedeviceinput * input = [avcapturedeviceinput deviceinputwithdevice:device error:nil];
- [Session Addinput:input];
- //Create video output and add to current session
- Avcapturevideodataoutput * output = [[Avcapturevideodataoutput alloc]init];
- [Session Addoutput:output];
- //Start session configuration
- [Session beginconfiguration];
- [Device Lockforconfiguration:nil];
- //Set torch to On
- [Device Settorchmode:avcapturetorchmodeon];
- [Device unlockforconfiguration];
- [Session commitconfiguration];
- //Start the session
- [Session startrunning];
- //Keep the session around
- [Self setavsession:self. Avsession];
- [Output RELEASE];
- }
- }
- -(void) Closeflashlight
- {
- [Self. Avsession stoprunning];
- [Self. Avsession release];
- }
iOS development calls system camera and turns on flash