1

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
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
David Holmes
  • 223
  • 1
  • 2
  • 12
  • Please see my answer here for future reference: http://stackoverflow.com/questions/3190034/turn-on-torch-flash-on-iphone-4/3367424#3367424 – iwasrobbed Jul 29 '10 at 22:19

2 Answers2

1

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
  • 45,586
  • 12
  • 116
  • 142
1

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
  • 8,496
  • 3
  • 36
  • 68