Code is relatively simple, just a few lines, no nonsense, also do not explain, directly on the code bar.
1. import Header File
1 #import <AVFoundation/AVFoundation.h>
2, click the button to achieve:
1-(Ibaction) Torchontouchbutton: (UIButton *) sender{2Class Capturedeviceclass = nsclassfromstring (@"Avcapturedevice");//Equipment Class3 if(Capturedeviceclass! =Nil) {4Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];//input5 if([Device Hastorch]) {//determine if there is a flash6[Device Lockforconfiguration:nil];//request exclusive access to hardware devices7 if(Sender.tag = =0) {8Sender.tag =1;9[Device Settorchmode:avcapturetorchmodeon];//Flashlight OpenTen}Else{ OneSender.tag =0; A[Device Settorchmode:avcapturetorchmodeoff];//Flashlight Off - } -[Device unlockforconfiguration];//request exclusive access to hardware devices the } - } -}
Turn Flashlight on/off