IOS第三十四天——一个简单的手电筒应用
2013-08-21 22:38
405 查看
前阵子被热死了。。。所以懒了好久,
其实我偷懒追了一下爱看的美剧,然后还学了点儿python,然后就么有了,现在言归正传,得空写了个手电筒的小应用,没有啥美化,就是实现了最基础的一个功能,布局也没啥好说的,就是只有一个图片按钮,所以这里直接贴代码:
// EricFlashlight
//
// Created by EricTang on 13-8-21.
// Copyright (c) 2013年 Eric Tang. All rights reserved.
//
#import "ETViewController.h"
@interface ETViewController ()
@end
@implementation ETViewController
@synthesize isLightOn;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
isLightOn=NO;
AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (![device hasTorch])
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"闪光灯" message:@"你么有闪光灯呢亲" delegate:self cancelButtonTitle:@"好吧" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//打开LED闪光灯
-(void)turnOnLed:(bool)update
{
AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch])
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
if (update)
{
[_btnOnOff setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];
isLightOn=YES;
}
}
//关闭LED闪光灯
-(void)turnOffLed:(bool)update
{
AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch])
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
if (update)
{
[_btnOnOff setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
isLightOn=NO;
}
}
- (void)dealloc
{
[_btnOnOff release];
[super dealloc];
}
- (IBAction)onBtnShine:(id)sender
{
if (isLightOn)
{
[self turnOffLed:YES];
}
else
{
[self turnOnLed:YES];
}
}
@end
重点是记住,需要添加AVFoundation.framework,其他的就没啥了,也没做啥特殊的优化或者说弄个频闪啥的,就是最纯粹的一个手电筒的应用,留作以后备用。
2013年8月21日,Eric.Tang 记
其实我偷懒追了一下爱看的美剧,然后还学了点儿python,然后就么有了,现在言归正传,得空写了个手电筒的小应用,没有啥美化,就是实现了最基础的一个功能,布局也没啥好说的,就是只有一个图片按钮,所以这里直接贴代码:
// EricFlashlight
//
// Created by EricTang on 13-8-21.
// Copyright (c) 2013年 Eric Tang. All rights reserved.
//
#import "ETViewController.h"
@interface ETViewController ()
@end
@implementation ETViewController
@synthesize isLightOn;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
isLightOn=NO;
AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (![device hasTorch])
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"闪光灯" message:@"你么有闪光灯呢亲" delegate:self cancelButtonTitle:@"好吧" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//打开LED闪光灯
-(void)turnOnLed:(bool)update
{
AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch])
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
if (update)
{
[_btnOnOff setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];
isLightOn=YES;
}
}
//关闭LED闪光灯
-(void)turnOffLed:(bool)update
{
AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch])
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
if (update)
{
[_btnOnOff setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
isLightOn=NO;
}
}
- (void)dealloc
{
[_btnOnOff release];
[super dealloc];
}
- (IBAction)onBtnShine:(id)sender
{
if (isLightOn)
{
[self turnOffLed:YES];
}
else
{
[self turnOnLed:YES];
}
}
@end
重点是记住,需要添加AVFoundation.framework,其他的就没啥了,也没做啥特殊的优化或者说弄个频闪啥的,就是最纯粹的一个手电筒的应用,留作以后备用。
2013年8月21日,Eric.Tang 记
相关文章推荐
- iOS开发UI篇—实现一个简单的手势解锁应用(基本) - 文顶顶
- 一个小时写一个简单的iOS新闻应用
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
- Appium+Robotframework实现iOS应用的自动化测试-5:一个简单的例子
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
- Swift教程第二部分: 一个简单的iOS应用
- 为一个 iOS 应用编写一个简单的 Node.js/MongoDB Web 服务
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
- iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例
- iOS开发UI篇—实现一个简单的手势解锁应用(基本)
- Appium+Robotframework实现iOS应用的自动化测试-5:一个简单的例子
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 - 文顶顶
- Swift教程第二部分: 一个简单的iOS应用
- ios学习笔记block回调的应用(一个简单的例子)
- 【iOS开发】 CoreText 使用教程:以创建一个简单的杂志应用为例