使用以下function即可, 

若要開啟則傳入true之布林值即可,

呼叫範例如下,

[self turnTorchOn:YES];
[self turnTorchOn:NO];

完成程式碼如下:

- (void) turnTorchOn: (bool) on
{
    
    // check if flashlight available
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
    if (captureDeviceClass != nil)
    {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch] && [device hasFlash])
        {
            [device lockForConfiguration:nil];
            if (on)
            {
                [device setTorchMode:AVCaptureTorchModeOn];
                [device setFlashMode:AVCaptureFlashModeOn];
                //torchIsOn = YES; //define as a variable/property if you need to know status
            } else {
                [device setTorchMode:AVCaptureTorchModeOff];
                [device setFlashMode:AVCaptureFlashModeOff];
                //torchIsOn = NO;
            }
            [device unlockForConfiguration];
        }
    }
}
arrow
arrow
    文章標籤
    objectiveC iOS 閃光燈
    全站熱搜
    創作者介紹
    創作者 Will 的頭像
    Will

    Will的部落格

    Will 發表在 痞客邦 留言(4) 人氣()