0

我在 xcode 上有一个手电筒项目,但我不知道如何向它添加亮度控制。我有隐藏手电筒的打开和关闭部分的按钮,我使用 imageviews 显示预渲染图像。我在下面发布我的代码,所有帮助将不胜感激。

Viewcontroller.h-  
is basically the initializations of the buttons, IBActions, IBOutlets and Properties


ViewController.m



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize offButton,onview,onButton,offview,slider;

-(IBAction)torchon:(id)sender
{

    onButton.hidden=YES;
    offButton.hidden=NO;
    onview.hidden=NO;
    offview.hidden=YES;


    AVCaptureDevice *device=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if([device isTorchAvailable]&&[device isTorchModeSupported:AVCaptureTorchModeOn])
    {
        BOOL sucessful=[device  lockForConfiguration:nil];
        if(sucessful)
        {
            [device  setTorchModeOnWithLevel:slider.value error:NULL];

            [device  unlockForConfiguration];
        }
    }
}

-(IBAction)torchoff:(id)sender
{
    AVCaptureDevice *flashlight=[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if([flashlight isTorchAvailable]&&[flashlight isTorchModeSupported:AVCaptureTorchModeOn])
    {
        BOOL sucessful=[flashlight lockForConfiguration:nil];
        if(sucessful)
        {
            [flashlight setTorchMode:AVCaptureTorchModeOff];

            [flashlight unlockForConfiguration];

        }
    }

    onButton.hidden=NO;
    offButton.hidden=YES;
    onview.hidden=YES;
    offview.hidden=NO;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    onButton.hidden=YES;
    offButton.hidden=NO;
    onview.hidden=NO;
    offview.hidden=YES;
    AVCaptureDevice *device =[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if([device  isTorchAvailable]&&[device  isTorchModeSupported:AVCaptureTorchModeOn])
    {
        BOOL sucessful=[device  lockForConfiguration:nil];
        if(sucessful)
        {
            [device  setTorchMode:AVCaptureTorchModeOn];

            [device  unlockForConfiguration];
        }
    }
}
-(IBAction)slideing:(id)sender
{
    slider.maximumValue = 1.0f;
    slider.minimumValue = 0.0f;
    [slider setContinuous:YES];
    [slider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];
    [device setTorchModeOnWithLevel:slider.value error:NULL];
    [device unlockForConfiguration];


}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated

}
@end



and nothing in appdelegate.m and appdelegate.h

i have created my objects like a slider, and button as well as the image views in a storyboard and have linked them to my code. I've used IBAction functions and have used interface builder a bit as well. As i said before, i cant figure out how to add a shake to switch on and off feature. Any help is appreciated!
4

0 回答 0