How to Turn the iPhone Camera Flash On/Off

How to turn the iPhone camera flash on/off swift 2?


@IBAction func didTouchFlashButton(sender: UIButton) {
let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

// check if the device has torch
if avDevice.hasTorch {
// lock your device for configuration
do {
let abv = try avDevice.lockForConfiguration()
} catch {
print("aaaa")
}

// check if your torchMode is on or off. If on turns it off otherwise turns it on
if avDevice.torchActive {
avDevice.torchMode = AVCaptureTorchMode.Off
} else {
// sets the torch intensity to 100%
do {
let abv = try avDevice.setTorchModeOnWithLevel(1.0)
} catch {
print("bbb")
}
// avDevice.setTorchModeOnWithLevel(1.0, error: nil)
}
// unlock your device
avDevice.unlockForConfiguration()
}
}

How to turn on/offf camera flash light with ARKit?


func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video)
else {return}

if device.hasTorch {
do {
try device.lockForConfiguration()

if on == true {
device.torchMode = .on // set on
} else {
device.torchMode = .off // set off
}

device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}

Call this as

toggleTorch(on: true) of toggleTorch(on: false)

ref: Hacking with Swift

Switch On/Off flash while recording the Video in iphone app

Assuming you are using UIImagePickerController (from your tag), use the cameraFlashMode provided by UIImagePickerController to control the flash.

You can set its value to UIImagePickerControllerCameraFlashModeOff, UIImagePickerControllerCameraFlashModeAuto or UIImagePickerControllerCameraFlashModeOn. Default is auto.

How can i disable iphone camera auto-flash function?

If you are setting your camera capture session with AV Foundation:

Tell the AVCaptureDevice to lockForConfiguration, then tell it to setFlashMode

Read the documentation since this is a very simple task.

http://developer.apple.com/library/ios/#DOCUMENTATION/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188

Edit: What you are looking for is explicitly written under the "media capture" section from the link I posted.

How to turn off the flash light while scanning using ipod

you can do something like,

 AVCaptureDevice *mDevice = mReader.readerView.device;
[mDevice setTorchModeOnWithLevel:0.5 error:nil]; // you can use different torchmode!

or

 [myDevice setTorchMode:AVCaptureTorchModeOff]; // off torchmode

Dopn't forget to import AVFoundation.

You can refer this answewr or this answer for more details.

Hope this will help :)

iOS 10 Camera flash ON/OFF not working

Issue solved on iOS 10.2 .
Now manual camera flash mode handling works fine again, without need of enabling and disabling showsCameraControls.

How can the rear camera flash be forced to on in Swift 3?

Set the camera flash mode in a completion closure as follows:

  self.present(imagePicker, animated: true)
{
imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.on
}

The full source is as follows:

  let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear;

self.present(imagePicker, animated: true)
{
imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.on
}

Verified that this works with Swift 3, but not sure if it helps for older versions.



Related Topics



Leave a reply



Submit