Enable System camera flash for IOS
Today, I will share with you how to call the iphone camera function and turn on the flash. I don't quite understand some code, and many of them learn from others on the Internet.
IOS has two ways to take photos and videos:
1. Use UIImagePickerController directly. This class provides a simple and convenient photo taking and Image Selection Function.
2. The other is to use the AVFoundation. framework to completely customize the photo interface and select the image library interface. If I only use the first method, I will introduce the first method:
1. Before calling the interface, we need to determine whether the current device supports UIImagePickerController and use isSourceTypeAvailable: To determine whether the interface is available.
2. Check the media type. In this case, we call availableMediaTypeForSourceType to determine
When calling UIImagePickerController, we need to add two proxy methods:
UINavigationControllerDelegate and UIImagePickerControllerDelegate, you can also tune the flashlight when calling the camera, which will be included in the Code later.
To call the flashlight, you must first create an AVCaptureSession instance object:
The Code is as follows:
// Created by sheets original on 13-1-23.
// Copyright (c) 2013 Zhang yiyuan. All rights reserved.
//
# Import
// Call the flashlight call framework
# Import
@ Interface CameraViewController: UIViewController
{
AVCaptureSession * _ AVSession; // class created when the flashlight is called
}
@ Property (nonatomic, retain) AVCaptureSession * AVSession;
@ End
In. in m's-(void) viewDidLoad, a 4 Button is created. Camera calls the Camera, Library calls the image Library, flashlight enables the flashlight, and close the flashlight. The code for creating the Button here will no longer be written.
The Code is as follows:
// Open the camera
-(Void) addCarema
{
// Determine whether the camera can be turned on. The simulator cannot use this function.
If ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
Picker. delegate = self;
Picker. allowsEditing = YES; // editable or not
// 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 camera" delegate: nil cancelButtonTitle: @ "Drat! "OtherButtonTitles: nil];
[Alert show];
}
}
After opening the camera, you need to call the method in UIImagePickerControllerDelegate, the method to be executed after the shooting is complete, and the method to be executed after clicking Cancel:
The Code is as follows:
// Method to be executed after shooting is complete
-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info
{
// Obtain the image
UIImage * image = [info objectForKey: UIImagePickerControllerOriginalImage];
// Save the image to the album
UIImageWriteToSavedPhotosAlbum (image, nil );
[Self dismissModalViewControllerAnimated: YES];
}
// Click the Cancel button to execute the command.
-(Void) imagePickerControllerDidCancel :( UIImagePickerController *) picker
{
[Self dismissModalViewControllerAnimated: YES];
}
You have completed calling the camera photo and saving it to the image library.
Next we will introduce how to open the Photo Library:
The Code is as follows:
-(Void) openPicLibrary
{
// The album can be opened in a simulator.
If ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
Picker. delegate = self;
Picker. allowsEditing = YES; // whether it can be edited
// Open the album and select a photo
Picker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[Self presentModalViewController: picker animated: YES];
[Picker release];
} Else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Error" message: @ "you don't have a camera" 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];
}
I can't add comments to the Code for calling the flashlight because I don't understand it very well, but I have already tested it for use. However, there is a bug when I call the flashlight. The flashlight will be idle, then keep on going
The Code is as follows:
-(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 und
[Self setAVSession: self. AVSession];
[Output release];
}
}
-(Void) closeFlashlight
{
[Self. AVSession stopRunning];
[Self. AVSession release];
}
The above is all the content of this article. I hope you will like it.