码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 手电筒代码和理解

时间:2015-05-13 12:36:16      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

1、首先引入AVFoundation.framework框架

2、.h文件

添加#import <AVFoundation/AVFoundation.h>

 

@interface LightViewController : UIViewController

{

    BOOL isLightOn;

    AVCaptureDevice *device;

}

 

@property BOOL isLightOn;

@end

3、.m文件

 

#import "LightViewController.h"

 

@interface LightViewController ()

 

@end

 

@implementation LightViewController

@synthesize isLightOn;

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNilbundle:nibBundleOrNil];

    if (self) {

        self.navigationItem.title = @"手电筒";

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

 

    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [btn setFrame:CGRectMake(80, 150, 100, 80)];

    [btn addTarget:self action:@selector(btnClicked)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    //AVCaptureDevice代表抽象的硬件设备

     // 找到一个合适的AVCaptureDevice

    device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

    if (![device hasTorch]) {//判断是否有闪光灯

        UIAlertView *alter = [[UIAlertViewalloc]initWithTitle:@"提示" message:@"当前设备没有闪光灯,不能提供手电筒功能" delegate:nil cancelButtonTitle:@"Cancel"otherButtonTitles:nil, nil];

        [alter show];

        [alter release];

    }

    

    isLightOn = NO;

    

}

 

-(void)btnClicked

{

    isLightOn = 1-isLightOn;

    if (isLightOn) {

        [self turnOnLed:YES];

    }else{

        [self turnOffLed:YES];

    }

}

 

//打开手电筒

-(void) turnOnLed:(bool)update

{

    [device lockForConfiguration:nil];

    [device setTorchMode:AVCaptureTorchModeOn];

    [device unlockForConfiguration];

}

 

//关闭手电筒

-(void) turnOffLed:(bool)update

{

    [device lockForConfiguration:nil];

    [device setTorchMode: AVCaptureTorchModeOff];

    [device unlockForConfiguration];

}

iOS 手电筒代码和理解

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4499683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!