目前鏡頭旁邊的led燈似乎只有在iphone4才有,不過說真的這小小的led燈還真的蠻好用的,在app store上也可以看到一大堆類似的軟體。
step1.在.h檔內先將framework給import進來
//import framework
#import <AVFoundation/AVFoundation.h>
//宣告變數並將這變數交由property管理
AVCaptureSession *torchSession;
@property (nonatomic, retain) AVCaptureSession *torchSession;
step2.回到.m檔內記得在@@implementation及@end間加上synthesize
@synthesize torchSession;
step3.開關LED燈的函數
-(void) openLED{
NSLog(@"click openLED");
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
if (device.torchMode == AVCaptureTorchModeOff) {
NSLog(@"On");
AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[session addInput:flashInput];
[session addOutput:output];
[device unlockForConfiguration];
[output release];
[session commitConfiguration];
[session startRunning];
[self setTorchSession:session];
[session release];
}
else {
NSLog(@"Off");
[torchSession stopRunning];
}
}
}
step4.記得在dealloc把宣告過的東西release掉
- (void)dealloc {
[torchSession release];
[super dealloc];
}
step5.加入framework
step6.開始玩LED燈
相關連結
iOS Developer Library
iPhone4 flashlight on/off LED燈開關
訂閱:
文章 (Atom)