您的位置:首页 > 移动开发 > IOS开发

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 记
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS
相关文章推荐