在 iOS 上使用裝置(')的 Flash 單元
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 AR 工作階段中啟用裝置的閃光燈,有助於改善可見度。
確認目前的相機設定是否支援閃光燈
並非所有相機設定都支援啟用閃光燈。在啟用閃光燈或提供使用者啟用閃光燈的選項之前,請確認閃光燈單元可用於目前的相機設定:
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
return device.hasTorch;
}
return false;
啟用閃光燈
使用 AVCaptureTorchModeOn
設定 AR 工作階段,啟用閃光燈單元:
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
if (device.hasTorch) {
NSError *error = nil;
if ([device lockForConfiguration:&error]) {
device.torchMode = AVCaptureTorchModeOn;
[device unlockForConfiguration];
} else {
return;
}
}
}
停用閃光燈
使用 AVCaptureTorchModeOff
設定 AR 工作階段,停用閃光燈單元:
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
if (device.hasTorch) {
NSError *error = nil;
if ([device lockForConfiguration:&error]) {
device.torchMode = AVCaptureTorchModeOff;
[device unlockForConfiguration];
} else {
return;
}
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003eUsing the device's flash during an AR session can enhance visibility in low-light conditions.\u003c/p\u003e\n"],["\u003cp\u003eBefore enabling the flash, verify if the active camera configuration supports it by checking the \u003ccode\u003ehasTorch\u003c/code\u003e property of the \u003ccode\u003eAVCaptureDevice\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eEnable the flash by setting the \u003ccode\u003etorchMode\u003c/code\u003e property of the \u003ccode\u003eAVCaptureDevice\u003c/code\u003e to \u003ccode\u003eAVCaptureTorchModeOn\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eDisable the flash by setting the \u003ccode\u003etorchMode\u003c/code\u003e property of the \u003ccode\u003eAVCaptureDevice\u003c/code\u003e to \u003ccode\u003eAVCaptureTorchModeOff\u003c/code\u003e.\u003c/p\u003e\n"]]],["To utilize the device's flash in an AR session, first verify if the active camera configuration supports it using `device.hasTorch`. If supported, enable the flash by setting `device.torchMode` to `AVCaptureTorchModeOn` after locking the device for configuration. If the camera doesn't support flash, setting `AVCaptureTorchModeOn` will have no effect. To disable it set the `device.torchMode` to `AVCaptureTorchModeOff` and unlock the configuration.\n"],null,["# Use the device's flash unit on iOS\n\n\u003cbr /\u003e\n\nEnabling the device's flash unit during an AR session can help improve\nvisibility.\n\nCheck that the current camera configuration supports flash\n----------------------------------------------------------\n\nNot all camera configurations support enabling a flash unit.\nBefore enabling the flash or offering users the option to enable the flash,\nensure that the flash unit is available for the active camera\nconfiguration: \n\n AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];\n if (device) {\n return device.hasTorch;\n }\n return false;\n\nEnable the flash unit\n---------------------\n\nEnable the flash unit by configuring the AR session with\n[`AVCaptureTorchModeOn`](https://developer.apple.com/documentation/avfoundation/avcapturetorchmode/avcapturetorchmodeon): \n\n AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];\n if (device) {\n if (device.hasTorch) {\n NSError *error = nil;\n if ([device lockForConfiguration:&error]) {\n device.torchMode = AVCaptureTorchModeOn;\n [device unlockForConfiguration];\n } else {\n return;\n }\n }\n }\n\n| **Note:** Configuring [`AVCaptureTorchModeOn`](https://developer.apple.com/documentation/avfoundation/avcapturetorchmode/avcapturetorchmodeon) with a camera configuration that does not support a flash unit will have no effect.\n\nDisable the flash unit\n----------------------\n\nDisable the flash unit by configuring the AR session with\n[`AVCaptureTorchModeOff`](https://developer.apple.com/documentation/avfoundation/avcapturetorchmode/avcapturetorchmodeoff): \n\n AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];\n if (device) {\n if (device.hasTorch) {\n NSError *error = nil;\n if ([device lockForConfiguration:&error]) {\n device.torchMode = AVCaptureTorchModeOff;\n [device unlockForConfiguration];\n } else {\n return;\n }\n }\n }"]]