IOS turns on the flash on the system camera

  • 2020-05-07 20:30:11
  • OfStack

IOS has two ways of taking photos and videos:

1. Directly use UIImagePickerController, this class provides a simple and convenient function to take photos and select pictures in the library.

2. The other one is to completely customize the photo taking interface and photo library selection interface through the AVFoundation.framework framework. I've only done the first one, so I'm going to introduce you to the first one:

1. Before calling the interface first, we need to determine whether the current device supports UIImagePickerController, and use isSourceTypeAvailable: to determine whether it is available

2. We call availableMediaTypeForSourceType: judge when we view the media types that match

When we call UIImagePickerController, we need to add two of its proxy methods:

UINavigationControllerDelegate and UIImagePickerControllerDelegate can also adjust the flash when calling the camera.

To call the flash, you need to create an instance object of the AVCaptureSession class:


//  Created by A exploration of the original on 13-1-23.
//  Copyright (c) 2013 years A exploration of the original . All rights reserved.
//
 
#import <UIKit/UIKit.h>
// Call the flash to call the framework
#import <AVFoundation/AVFoundation.h>
 
@interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    AVCaptureSession * _AVSession;// Class created when the flash is called
}
 
@property(nonatomic,retain)AVCaptureSession * AVSession;
 
@end

Create 4Button in the.m - (void)viewDidLoad, Camera calls the camera, Library calls the photo library, flashlight turns on the flash, close turns off the flash, I won't write the code to create Button here.


// Open the camera
-(void)addCarema
{
    // To determine whether the camera can be opened, the simulator cannot be used
    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 execute after shooting and the method to execute after clicking Cancel:


// The method to be executed after the shooting is completed
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Get pictures
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    // Save the pictures in the album
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [self dismissModalViewControllerAnimated:YES];
}
// Click on the Cancel Execute the method after the button
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissModalViewControllerAnimated:YES];
}

Calling the camera photo and saving it to the gallery has been completed.

Next, open the photo library:


-(void)openPicLibrary
{
    // The album can be opened with an emulator
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;// Can I edit it?
        // Open the album and 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 camera. " delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
        [alert show];
    }
}
// Select the proxy method for image entry
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self dismissModalViewControllerAnimated:YES];
}

I can't comment the code that calls the flash, because I don't understand it very well, but it is available for testing. However, when I adjust the flash, 1 is bug, and the flash will be idle for 1, and then 1 will be on


-(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];
}

The above is all the content of this article, I hope you can enjoy it.


Related articles: