Activate the LED for a flashlight app

后端 未结 2 629
死守一世寂寞
死守一世寂寞 2020-12-24 04:06

I\'m trying to make a flashlight app for my iPhone. I have an iPhone 4 and would like to utilize the LED on my iPhone for my project. Can anyone help me to get started with

相关标签:
2条回答
  • 2020-12-24 04:52

    Here is a shorter version you can now use to turn the LED on or off:

    - (void)torchOnOff: (BOOL) onOff
    {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch]) {
            [device lockForConfiguration:nil];
            [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
            [device unlockForConfiguration];
        }
    }
    

    UPDATE: (March 2015)

    You can also set the brightness of the torch:

    - (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];
        }
    }
    
    0 讨论(0)
  • 2020-12-24 04:55

    Use the following:

    AVCaptureSession * session = [[AVCaptureSession alloc] init];
    
    [session beginConfiguration];
    
    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    if ([device hasTorch] && [device hasFlash]){
    [device lockForConfiguration:nil];
    [device setTorchMode:AVCaptureTorchModeOn];
    [device setFlashMode:AVCaptureFlashModeOn]; 
    [device unlockForConfiguration];
    
    AVCaptureDeviceInput * flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    if (flashInput){
        [session addInput:flashInput];
    }
        AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc] init];
        [session addOutput:output];
            [output release];
        [session commitConfiguration];  
        [session startRunning];
    }
    [self setTorchSession:session];
    [session release];
    

    (From a discussion on iPhoneDevSDK)

    0 讨论(0)
提交回复
热议问题