views:

430

answers:

2

So I have made a custom UIButton and added it to the code and made the connections in interfacebuiler. I want the button to work as a on and off switch, how do I do this correctly? I'm a beginner at iphone development and this is for a school project for this class I'm taking during the summer to get a head start for next semester.

So if anyone can help me understand how to do this the right way and maybe write comments in the code. Thanks for all the help. David H.

Here's my code:

//
//  FlashlightViewController.h
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FlashlightViewController : UIViewController {

    AVCaptureSession *torchSession;
    IBOutlet UIButton *button;
}

-(IBAction)pressButton:(id) sender;

@property (nonatomic, retain) AVCaptureSession *torchSession;
@property (nonatomic, retain) IBOutlet UIButton *button;

@end

Here is the .m file

//
//  FlashlightViewController.m
//

#import "FlashlightViewController.h"

@implementation FlashlightViewController
@synthesize torchSession;
@synthesize button;


- (void)viewDidLoad {

    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];

    [super viewDidLoad];
}
- (void)viewDidUnload {
    self.button = nil;
}

- (void)dealloc {
    [TorchSession release];
    [button release];
    [super dealloc];
}
-(IBAction)pressButton : (id) sender{

}
@end
+1  A: 

What is your problem actually? There is a built in UISwitch object. And if you want to create a custom one, then you can keep track of a bool flag and toggle that in the button handler.

taskinoor
+1  A: 

In your @interface section (.h), you also need IBOutlet IBAction pressButton;. Then, in Interface Builder, in the inspector (outlet section), select File's Owner, and connect pressButton: to the UIButton's Touch Up Inside action.

To toggle the torch state, add BOOL torchAlreadyOn; to the @interface section (.h). Then, move your viewDidLoad custom code to the pressButton method. Then, at the end of the pressButton method, add:

if (torchAlreadyOn) {
    torchAlreadyOn = NO;
}
else {
    torchAlreadyOn = YES;
}

Then, everywhere where you set the torch state to on, enclose it in an if...else statement which checks the BOOL:

AVCaptureSession *session = [[AVCaptureSession alloc] init];

[session beginConfiguration];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if ([device hasTorch] && [device hasFlash]){
    [device lockForConfiguration:nil];

    if (torchAlreadyOn) {
        [device setTorchMode:AVCaptureTorchModeOn];
        [device setFlashMode:AVCaptureFlashModeOn];
    }
    else {
        [device setTorchMode:AVCaptureTorchModeOff];
        [device setFlashMode:AVCaptureFlashModeOff];
    }
    [device unlockForConfiguration];

    AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
    if (flashInput){
        if (!torchAlreadyOn) {
            [session addInput:flashInput];
        }
        else {
            [session removeInput:flashInput];
        }
    }

    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    if (!torchAlreadyOn) {
        [session addOutput:output];
    }
    else {
        [session removeOutput:output];
    }
    [output release];
    [session commitConfiguration];

    [session startRunning];
}

[self setTorchSession:session];
[session release];
jrtc27
By the way, can you please accept my answer for your other question regarding this? Thanks
jrtc27
jrtc27, is there anyway to control the heat when the flash is on? the device get really hot!!!Or is it just they way it is?
David Holmes
Sadly, the heat is out of your control, as the LED is either on or off.
jrtc27
Just an fyi.. just tried this snippet and it seems to freeze up the app when you toggle it off.
iWasRobbed
Please see my answer here for future reference: http://stackoverflow.com/questions/3190034/turn-on-torch-flash-on-iphone-4/3367424#3367424
iWasRobbed
Posted this off the top of my head - kudos @IWasRobbed - +2
jrtc27