Use the device's flash unit on iOS

  • You can improve visibility in an AR session by enabling the device's flash unit.

  • Before enabling the flash, check if the current camera configuration supports it.

  • You can enable the flash unit by configuring the AR session with AVCaptureTorchModeOn .

  • You can disable the flash unit by configuring the AR session with AVCaptureTorchModeOff .

Enabling the device's flash unit during an AR session can help improve visibility.

Check that the current camera configuration supports flash

Not all camera configurations support enabling a flash unit. Before enabling the flash or offering users the option to enable the flash, ensure that the flash unit is available for the active camera configuration:

  AVCaptureDevice 
  
 * 
 device 
  
 = 
  
 [ 
 AVCaptureDevice 
  
 defaultDeviceWithMediaType 
 : 
 AVMediaTypeVideo 
 ]; 
 if 
  
 ( 
 device 
 ) 
  
 { 
  
 return 
  
 device 
 . 
 hasTorch 
 ; 
 } 
 return 
  
 false 
 ; 
 

Enable the flash unit

Enable the flash unit by configuring the AR session with AVCaptureTorchModeOn :

  AVCaptureDevice 
  
 * 
 device 
  
 = 
  
 [ 
 AVCaptureDevice 
  
 defaultDeviceWithMediaType 
 : 
 AVMediaTypeVideo 
 ]; 
 if 
  
 ( 
 device 
 ) 
  
 { 
  
 if 
  
 ( 
 device 
 . 
 hasTorch 
 ) 
  
 { 
  
 NSError 
  
 * 
 error 
  
 = 
  
 nil 
 ; 
  
 if 
  
 ([ 
 device 
  
 lockForConfiguration 
 : 
& error 
 ]) 
  
 { 
  
 device 
 . 
 torchMode 
  
 = 
  
 AVCaptureTorchModeOn 
 ; 
  
 [ 
 device 
  
 unlockForConfiguration 
 ]; 
  
 } 
  
 else 
  
 { 
  
 return 
 ; 
  
 } 
  
 } 
 } 
 

Disable the flash unit

Disable the flash unit by configuring the AR session with AVCaptureTorchModeOff :

  AVCaptureDevice 
  
 * 
 device 
  
 = 
  
 [ 
 AVCaptureDevice 
  
 defaultDeviceWithMediaType 
 : 
 AVMediaTypeVideo 
 ]; 
 if 
  
 ( 
 device 
 ) 
  
 { 
  
 if 
  
 ( 
 device 
 . 
 hasTorch 
 ) 
  
 { 
  
 NSError 
  
 * 
 error 
  
 = 
  
 nil 
 ; 
  
 if 
  
 ([ 
 device 
  
 lockForConfiguration 
 : 
& error 
 ]) 
  
 { 
  
 device 
 . 
 torchMode 
  
 = 
  
 AVCaptureTorchModeOff 
 ; 
  
 [ 
 device 
  
 unlockForConfiguration 
 ]; 
  
 } 
  
 else 
  
 { 
  
 return 
 ; 
  
 } 
  
 } 
 } 
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: