IOS 调用iphone闪光灯

添加:AVFoundation库

#import <AVFoundation/AVFoundation.h>

.h


@interface xxxxx : UIViewController {
AVCaptureSession *AVSession;
}
@property(nonatomic,retain) AVCaptureSession *AVSession;


.m
@synthesize AVSession;
.......
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaTy pe:AVMediaTypeVideo];
 
if (device.torchMode == AVCaptureTorchModeOff)
{
// Create an AV session
AVCaptureSession *session = [[AVCaptureSession alloc] init];
 
// Create device input and add to current session
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
[session addInput:input];
 
// Create video output and add to current session  
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
 
// Start session configuration
[session beginConfiguration];
[device lockForConfiguration:nil];
 
// Set torch to on
[device setTorchMode:AVCaptureTorchModeOn];
 
[device unlockForConfiguration];
[session commitConfiguration];
 
 
// Start the session
[session startRunning];
 
// Keep the session around
[self setAVSession:AVSession];
 
[output release];
}
else
{
[AVSession stopRunning]; //AVSession是定义在头文件的 (属性)
[AVSession release], AVSession = nil;
}

你可能感兴趣的:(调用iphone闪光灯)