iphone手电筒
来源:互联网 发布:国家软件测试中心 编辑:程序博客网 时间:2024/04/29 12:11
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];
}
- iphone手电筒
- 手电筒
- Forms 手电筒
- LED手电筒
- android手电筒
- 手电筒源码
- Android手电筒
- Android手电筒
- 手电筒功能
- 手电筒开发
- android 手电筒
- android手电筒
- 制作手电筒
- android手电筒
- iOS 手电筒
- android 手电筒
- 手机手电筒
- Android手电筒
- Android音频系统之AudioFlinger(一)
- IIS 如何设置首页
- Linux 中 RPM 命令参数使用详解
- php curl的简单使用
- javascript右下角弹层及自动隐藏
- iphone手电筒
- php中strpos函数的使用
- Navicat for mysql 10.1.7 注册码
- android 邮件 总结
- Codeforces Round #213 (Div. 2)
- 揭开Linux操作系统的Swap交换区之谜
- Android音频系统之AudioFlinger(二)
- NGFF接口标准, 或在今年大放异彩
- 唉 帐号都这么久了 还是没升级 很忙情况啊