How to use AVCaptureFlashMode

I am creating an application to allow me to stitch images together into a panoramic scene. I want to be able to enable Flash Flash on iPhone 4 programmatically.

How can i do this?

I read the documentation and found that I need to use AVCaptureFlashMode

but i cant figure out how to use it?

Any help would be appreciated.


Updated code below. Thanks SIF!

NSError * error = nil;
    NSLog (@ "Setting up LED");

    if ([captDevice hasTorch] == NO)
    {
        NSLog (@ "Error: This device doesnt have a torch");
    }
    if ([captDevice isTorchModeSupported: AVCaptureTorchModeOn] == NO)
    {
        NSLog (@ "Error: This device doesnt support AVCaptureTorchModeOn");
    }

    AVCaptureSession * captureSession = [[AVCaptureSession alloc] init];
    AVCaptureDeviceInput * videoInput = [[AVCaptureDeviceInput alloc] initWithDevice: captDevice error: & error];
    AVCaptureVideoDataOutput * videoOutput = [[AVCaptureVideoDataOutput alloc] init];

    if (videoInput && videoOutput) 
    {
        [captureSession addInput: videoInput];
        [captureSession addOutput: videoOutput];
        if ([captDevice lockForConfiguration: & error])
        {
            if (flag == YES) {
                captDevice.torchMode = AVCaptureTorchModeOn;
            } else {
                captDevice.torchMode = AVCaptureTorchModeOff;
            }           
            [captDevice unlockForConfiguration];
        }
        else 
        {
            NSLog (@ "Could not lock device for config error:% @", error);
        }
        [captureSession startRunning];
    }
    else 
    {
        NSLog (@ "Error:% @", error);
    }

How to disable it?

+3
source share
1 answer
AVCaptureDevice* d = nil;

// find a device by position
NSArray* allDevices = [AVCaptureDevice devices];
for (AVCaptureDevice* currentDevice in allDevices) {
  if (currentDevice.position == AVCaptureDevicePositionBack) {
    d = currentDevice;
  }
}

// at this point, d may still be nil, assuming we found something we like....

NSError* err = nil;
BOOL lockAcquired = [d lockForConfiguration:&err];

if (!lockAcquired) {
   // log err and handle...
} else {
   // flip on the flash mode
   if ([d hasFlash] && [d isFlashModeSupported:AVCaptureFlashModeOn] ) {
      [d setFlashMode:AVCaptureFlashModeOn];
   }

   [d unlockForConfiguration];
}
+7
source

Source: https://habr.com/ru/post/1753708/


All Articles