iOS闪光灯操作

来源:互联网 发布:百度电影推荐系统算法 编辑:程序博客网 时间:2024/05/16 23:48

使用闪光灯需要引入AVFoundation框架,使用的类是AVCaptureSession , AVCaptureDevice , AVCaptureDeviceInput,然后在通过设置硬件属性,打开或关闭手电筒或flash就可以实现闪光灯效果,有demo,demo注释很清楚,肯定能让你初步明白怎么打开手电筒,demo链接http://download.csdn.net/download/lcg0412/6832689

一些设置属性:

AVCaptureDevice.h主要用来获取iphone一些关于相机设备的属性。
前置和后置摄像头

enum {

AVCaptureDevicePositionBack                = 1,

AVCaptureDevicePositionFront               = 2

};

typedef NSInteger AVCaptureDevicePosition;


闪光灯开关

 

enum {

AVCaptureFlashModeOff  = 0,

AVCaptureFlashModeOn   = 1,

AVCaptureFlashModeAuto = 2

};

typedef NSInteger AVCaptureFlashMode;


手电筒开关

 

enum {

AVCaptureTorchModeOff  = 0,

AVCaptureTorchModeOn   = 1,

AVCaptureTorchModeAuto = 2,

};

typedef NSInteger AVCaptureTorchMode;


焦距调整

 

enum {

AVCaptureFocusModeLocked              = 0,

AVCaptureFocusModeAutoFocus           = 1,

AVCaptureFocusModeContinuousAutoFocus = 2,

};

typedef NSInteger AVCaptureFocusMode;


曝光量调节

 

enum {

AVCaptureExposureModeLocked = 0,

AVCaptureExposureModeAutoExpose = 1,

AVCaptureExposureModeContinuousAutoExposure = 2,

};

typedef NSInteger AVCaptureExposureMode;


白平衡

 

enum {

AVCaptureWhiteBalanceModeLocked         = 0,

AVCaptureWhiteBalanceModeAutoWhiteBalance         = 1,

    AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance = 2,

};

typedef NSInteger AVCaptureWhiteBalanceMode;


//提醒- (void)alertView:(NSString *)text{    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:text message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];    [alertView show];    [alertView performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:@[@0, @YES] afterDelay:1];}//定时器,打开或关闭手电筒- (void)timerAction{    if(self.captureDevice.torchMode == AVCaptureTorchModeOn){        [self openTorch:AVCaptureTorchModeOff];    }    else{        [self openTorch:AVCaptureTorchModeOn];    }}//打开或关闭手电筒- (void)openTorch:(AVCaptureTorchMode)mode{    if(self.captureDevice.torchMode != mode){        //配置会话层        [self.captureSession beginConfiguration];        //对设备进行锁定操作        [self.captureDevice lockForConfiguration:NULL];        //打开手电筒        [self.captureDevice setTorchMode:mode];        //解锁        [self.captureDevice unlockForConfiguration];        //确认配置        [self.captureSession commitConfiguration];    }}//闪光灯效果- (IBAction)flashAction:(UISwitch *)sender {    if(![self.captureDevice hasTorch]){        [self alertView:@"亲,模拟器上没有手电筒哦。"];        return;    }    if(sender.on){        self.torchButton.enabled = NO;        if(self.timer == nil){            self.timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];        }        [self.timer fire];    }    else{        self.torchButton.enabled = YES;        [self.timer invalidate];        self.timer = nil;    }}//手电筒效果- (IBAction)torchOpen:(UISwitch *)sender {    if(![self.captureDevice hasTorch]){        [self alertView:@"亲,模拟器上没有手电筒哦。"];        return;    }    if(sender.on){        self.flashButton.enabled = NO;        [self openTorch:AVCaptureTorchModeOn];    }    else{        self.flashButton.enabled = YES;        [self openTorch:AVCaptureTorchModeOff];    }}- (AVCaptureDevice *)captureDevice{    if(_captureDevice == nil){        _captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];    }    return _captureDevice;}- (AVCaptureDeviceInput *)captureDeviceInput{    if(_captureDeviceInput == nil){        _captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self captureDevice] error:NULL];    }    return _captureDeviceInput;}- (AVCaptureSession *)captureSession{    if(_captureSession == nil){        _captureSession = [[AVCaptureSession alloc] init];        [_captureSession addInput:[self captureDeviceInput]];    }    return _captureSession;}

                                             
0 0
原创粉丝点击