Joh*_*ohn 45 iphone ios avcapturesession avcapturedevice
我知道打开闪光灯并在iPhone 4上保持打开的唯一方法是打开摄像机.我不太确定代码.这是我正在尝试的:
-(IBAction)turnTorchOn {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if (videoInput) {
[captureSession addInput:videoInput];
AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];
[captureSession addOutput:videoOutput];
[captureSession startRunning];
videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道这是否有效还是我错过了什么?(我还没有iPhone 4进行测试 - 只是尝试了一些新的API).
谢谢
mah*_*udz 72
这是一个较短的版本,您现在可以使用它打开或关闭灯:
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn]; // use AVCaptureTorchModeOff to turn off
[device unlockForConfiguration];
}
Run Code Online (Sandbox Code Playgroud)
更新:(2015年3月)
使用iOS 6.0及更高版本,您可以使用以下方法控制割炬的亮度或级别:
- (void)setTorchToLevel:(float)torchLevel
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
if (torchLevel <= 0.0) {
[device setTorchMode:AVCaptureTorchModeOff];
}
else {
if (torchLevel >= 1.0)
torchLevel = AVCaptureMaxAvailableTorchLevel;
BOOL success = [device setTorchModeOnWithLevel:torchLevel error:nil];
}
[device unlockForConfiguration];
}
}
Run Code Online (Sandbox Code Playgroud)
您可能还想监视返回值(success)setTorchModeOnWithLevel:.如果您尝试将水平设置得太高并且割炬过热,则可能会出现故障.在这种情况下,将水平设置为AVCaptureMaxAvailableTorchLevel将水平设置为给定火炬温度时允许的最高水平.
Tib*_*abo 36
iWasRobbed的答案很棒,除了AVCaptureSession一直在后台运行.根据Instrument的说法,在我的iPhone 4s上需要大约12%的CPU功率,因此我的应用程序在一分钟内耗费了大约1%的电量.换句话说,如果设备准备用于AV捕获,它并不便宜.
使用我的应用程序下面的代码需要每分钟0.187%,因此电池寿命超过5倍.
此代码适用于任何设备(在3GS(无闪存)和4s上测试).在模拟器中也测试了4.3.
#import <AVFoundation/AVFoundation.h>
- (void) turnTorchOn:(BOOL)on {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
torchIsOn = YES;
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
}
Run Code Online (Sandbox Code Playgroud)
iwa*_*bed 18
请参阅以下更好的答案: https ://stackoverflow.com/a/10054088/308315
老答案:
首先,在AppDelegate .h文件中:
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate : NSObject <UIApplicationDelegate> {
AVCaptureSession *torchSession;
}
@property (nonatomic, retain) AVCaptureSession * torchSession;
@end
Run Code Online (Sandbox Code Playgroud)
然后在你的AppDelegate .m文件中:
@implementation AppDelegate
@synthesize torchSession;
- (void)dealloc {
[torchSession release];
[super dealloc];
}
- (id) init {
if ((self = [super init])) {
// initialize flashlight
// test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
if (device.torchMode == AVCaptureTorchModeOff) {
NSLog(@"Setting up flashlight for later use...");
AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
[device lockForConfiguration:nil];
[session addInput:flashInput];
[session addOutput:output];
[device unlockForConfiguration];
[output release];
[session commitConfiguration];
[session startRunning];
[self setTorchSession:session];
[session release];
}
}
}
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
然后,只要你想打开它,只需执行以下操作:
// test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
}
Run Code Online (Sandbox Code Playgroud)
关闭它是类似的:
// test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
[device unlockForConfiguration];
}
Run Code Online (Sandbox Code Playgroud)
小智 15
将lockforConfiguration在你的代码,你宣布你的设置AVCaptureDevice是一个属性.
[videoCaptureDevice lockForConfiguration:nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
122975 次 |
| 最近记录: |