私は現在小さなプロジェクトに取り組んでいます。プロジェクト/アプリの意味は、デバイスを振るとフラッシュが点火するということです。もう一度振ると消灯!
ライトのオンとオフの間の最初の 3/4 トグルは正常に機能しますが、3/4 トグルの後、デバイスが揺れを検出しないため、ライトをオフにすることはできません。
もう 1 つの小さな問題 (iOS 5.0 以降) は、モーションが検出されると、フラッシュが非常に短く (1 秒) 点滅し、電源がオンになることです。これは短いフラッシュのように見えます。
私は何を間違っていますか
揺れの検出:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
NSLog(@"shaken, not stirred.");
[self playSound];
[self toggleFlashlight];
}
}
フラッシュ ライトの切り替え:
- (void)toggleFlashlight
{
AVCaptureDevice *device =
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
if (device.torchMode == AVCaptureTorchModeOff) {
NSLog(@"It's currently off.. turning on now.");
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 setAVSession:session];
[session release];
}
else {
NSLog(@"It's currently on.. turning off now.");
[AVSession stopRunning];
}
}
}
私のために問題を解決するためのあなたの専門知識に本当に感謝しています!