How does the iPhone implement the flashlight function?
First, define an avcapturesession instance object in the. h file.
Avcapturesession * torchsession;
Then, set the. M file as follows during initialization:
Avcapturedevice * capturedevice = [avcapturedevice defaultdevicewithmediatype: avmediatypevideo];
Avcapturedeviceinput * flashinput = [avcapturedeviceinput deviceinputwithdevice: capturedevice error: Nil];
Avcapturevideodataoutput * output = [[avcapturevideodataoutput alloc] init];
Avcapturesession * session = [avcapturesession alloc] init];
[Session beginconfiguration];
[Session addinput: flashinput];
[Session addoutput: output];
[Capturedevice lockforconfiguration: Nil];
Capturedevice. torchmode = avcapturetorchmodeon;
[Capturedevice unlockforconfiguration];
[Output release];
[Session commitconfiguration];
[Session startrunning];
[Self settorchsession: session];
[Session release];
Finally, the torchsession object is released in dealloc.