Activate the LED for a flashlight app

后端 未结 2 633
死守一世寂寞
死守一世寂寞 2020-12-24 04:06

I\'m trying to make a flashlight app for my iPhone. I have an iPhone 4 and would like to utilize the LED on my iPhone for my project. Can anyone help me to get started with

2条回答
  •  青春惊慌失措
    2020-12-24 04:52

    Here is a shorter version you can now use to turn the LED on or off:

    - (void)torchOnOff: (BOOL) onOff
    {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch]) {
            [device lockForConfiguration:nil];
            [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
            [device unlockForConfiguration];
        }
    }
    

    UPDATE: (March 2015)

    You can also set the brightness of the torch:

    - (void)setTorchToLevel:(float)torchLevel
    {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch]) {
            [device lockForConfiguration:nil];
            if (torchLevel <= 0.0) {
                [device setTorchMode:AVCaptureTorchModeOff];
            }
            else {
                if (torchLevel >= 1.0)
                    torchLevel = AVCaptureMaxAvailableTorchLevel;
                BOOL success = [device setTorchModeOnWithLevel:torchLevel   error:nil];
            }
            [device unlockForConfiguration];
        }
    }
    

提交回复
热议问题