2

我正在尝试为橘子酱做一个扩展,它可以打开和关闭 iPhone 中的相机闪光灯。我指的是关于使用相机闪光灯的这个答案。我已经把他提到的确切代码放在了上面,并且也[device setTorchMode:AVCaptureTorchModeOn];被调用了。但是闪光灯没有反应,好像什么都没发生过一样。有什么我需要做的,让它作为一个静态库工作,以便我可以在我的扩展中使用它吗?

更新:-
我正在使用 iOS-SDK 6.1 编译扩展并在 iPhone 4(iOS 版本 4.3.1)上进行测试。但现在我在 iPhone 5(iOS 版本 6.0.0)上测试,现在闪光灯打开了,但没有关闭。我想这可能会有所帮助。

4

3 回答 3

3

I don't know why, but the below code worked for me.

void TurnFlashOn_platform(bool turnOn) { 
    AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]){
        [device lockForConfiguration:nil]; //you must lock before setting torch mode
        [device setTorchMode:turnOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
        [device unlockForConfiguration];
    }
}

I guess must be some typos. Thanks anyways.

于 2013-05-16T12:27:36.747 回答
3

您复制的代码可能有错误。尝试以下操作:

-(void)turnOnFlash
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch]) {
            [device lockForConfiguration:nil];
            [device setTorchMode:AVCaptureTorchModeOff];
            [device unlockForConfiguration];
        }
}
于 2013-05-15T05:37:15.547 回答
2

Use this function to turn on and off the flash light...

#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];
    }
}
}
于 2013-05-15T05:52:27.853 回答