iOS development calls system camera and turns on flash

Source: Internet
Author: User
Tags close close

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
    1. #import <uikit/uikit.h>   
    2. //call the Flash call frame   
    3. #IMPORT&NBSP;<AVFOUNDATION/AVFOUNDATION.H>&NBSP;&NBSP;
    4.    
    5. @interface  CameraViewController : UIViewController< uinavigationcontrollerdelegate, uiimagepickercontrollerdelegate>  
    6. {   
    7.     avcapturesession * _avsession;
    8. }  
    9. &NBSP;&NBSP;&NBSP;
    10. @property (nonatomic,retain) avcapturesession * avsession;  
    11. Li class= "alt" >&NBSP;&NBSP;&NBSP;
    12. @end   

In. m-(void) Viewdidload build 4button,camera call camera, library call Photo Gallery, flashlight Open flash, close close flash

[Java]View Plaincopy
  1. Turn on the camera
  2. -(void) Addcarema
  3. {
  4. //Determine if camera can be turned on, simulator this function is not available
  5. if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
  6. Uiimagepickercontroller * Picker = [[Uiimagepickercontroller alloc]init];
  7. Picker.delegate = self;
  8. picker.allowsediting = YES; //whether editable
  9. //Camera
  10. Picker.sourcetype = Uiimagepickercontrollersourcetypecamera;
  11. [Self presentmodalviewcontroller:picker animated:yes];
  12. [Picker release];
  13. }else{
  14. //If the user is not prompted
  15. Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Error" message:@"You don't have a webcam" delegate:nil  cancelbuttontitle:@"drat!" otherbuttontitles:nil];
  16. [Alert show];
  17. }
  18. }


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
  1. The method to be executed after the shooting is completed
  2. -(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) Info
  3. {
  4. //Get Pictures
  5. UIImage * image = [info objectforkey:uiimagepickercontrolleroriginalimage];
  6. //images in albums
  7. Uiimagewritetosavedphotosalbum (image, nil, nil, nil);
  8. [Self dismissmodalviewcontrolleranimated:yes];
  9. }
  10. To execute a method after clicking the Cancel button
  11. -(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) Picker
  12. {
  13. [Self dismissmodalviewcontrolleranimated:yes];
  14. }


Call camera photo and save to Picture Library has completed.
Next, open photo Gallery:

[Java]View Plaincopy
  1. -(void) openpiclibrary
  2. {
  3. //albums can be opened with an emulator
  4. if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) {
  5. Uiimagepickercontroller * Picker = [[Uiimagepickercontroller alloc]init];
  6. Picker.delegate = self;
  7. picker.allowsediting = YES; //Whether you can edit
  8. //Open album Select Photos
  9. Picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
  10. [Self presentmodalviewcontroller:picker animated:yes];
  11. [Picker release];
  12. }else{
  13. Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Error" message:@"You don't have a webcam" delegate:nil  cancelbuttontitle:@"drat!" otherbuttontitles:nil];
  14. [Alert show];
  15. }
  16. }
  17. Select the proxy method for the image to enter
  18. -(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) image Editinginfo: (nsdictionary *) Editinginfo
  19. {
  20. [Self dismissmodalviewcontrolleranimated:yes];
  21. }


Code to call the Flash

[Java]View Plaincopy
  1. -(void) Openflashlight
  2. {
  3. Avcapturedevice * device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];
  4. if (Device.torchmode = = Avcapturetorchmodeoff) {
  5. //create an AV session
  6. Avcapturesession * session = [[Avcapturesession alloc]init];
  7. //Create device input and add to current session
  8. Avcapturedeviceinput * input = [avcapturedeviceinput deviceinputwithdevice:device error:nil];
  9. [Session Addinput:input];
  10. //Create video output and add to current session
  11. Avcapturevideodataoutput * output = [[Avcapturevideodataoutput alloc]init];
  12. [Session Addoutput:output];
  13. //Start session configuration
  14. [Session beginconfiguration];
  15. [Device Lockforconfiguration:nil];
  16. //Set torch to On
  17. [Device Settorchmode:avcapturetorchmodeon];
  18. [Device unlockforconfiguration];
  19. [Session commitconfiguration];
  20. //Start the session
  21. [Session startrunning];
  22. //Keep the session around
  23. [Self setavsession:self. Avsession];
  24. [Output RELEASE];
  25. }
  26. }
  27. -(void) Closeflashlight
  28. {
  29. [Self. Avsession stoprunning];
  30. [Self. Avsession release];
  31. }


iOS development calls system camera and turns on flash

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.