tags:

views:

179

answers:

1

Hi

I'm currently taking a summer class in iphone development this summer and our assignment this summer to make a flashlight app for our iphones. I have a iphone 4 and would like to utilize the LED on my iphone for my project, can anyone help me how to get started with that?

it would be very helpful if someone could point me in the right direction.

Thank you. David H.

+3  A: 

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)

jrtc27
Thank you, So I have added a button in the viewcontroller.xib in interfacebuilder. How do I make the button start and stop the flashlight?
David Holmes
Make an IBOutlet for an IBAction and connect it to the button's selector. Then, in the IBAction, depending on whether the LED is already on, call the code, or call a modified version of the code with all ons replaced with offs.
jrtc27
Have you checked my other post? I have added a IBOutlet and a IBActon but I don't know how to add my code to the button. Can you have a look and help me out and show me how to add the code to the button.I know I need to change;[device setTorchMode:AVCaptureTorchModeOn];[device setFlashMode:AVCaptureFlashModeOn];To[device setTorchMode:AVCaptureTorchModeOff];[device setFlashMode:AVCaptureFlashModeOff]; Can you look at my code in the other post and show me how to do this, you can email me holmes2870 @ yahoo.comI'm new to this so have patience...Thank youDavid H
David Holmes
Can you accept my answer please?
jrtc27
David: Can you please accept my answer like you did on your other question (click on the grey-outlined tick)? It gives me +15 reputation and you +2, and also will bring your accept rate up to 100%, meaning when you ask other questions, people will be more likely to answer, as they know that they are likely to have their answer accepted if it is correct. Many thanks
jrtc27
ok, jrtc27. Done.
David Holmes