Real Flashlight App for iPhone 4

I saw real flashlight app for iPhone in the App Store sold for like $0.99 or more and I thought that it couldn't be too hard to write. So here's what it takes to write a real flashlight app for iPhone 4. Btw, I'm not gonna tell you every step since I assume that you have at least a little bit of knowledge of how to write an iPhone application.

First, you just have to start an iPhone project with a "View-based Application" template. This project requires you to add an AVFoundation framework into the project. You can do that by right click the Frameworks folder and add the AVFoundation.framework.

Now you need to have a ViewController that you want to work with and follow these steps.
  • Import <avfoundation/avfoundation.h>
  • Create an AVCaptureSession instance variable that we'll use
  • Create two methods that will control the flash
Your code should look like this. However, I also connects my methods to the interface file using Interface Builder. Your code might look different.

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>


@interface FlashLightViewController : UIViewController {


AVCaptureSession *session;

}


- (IBAction)torchOn:(id)sender;

- (IBAction)torchOff:(id)sender;


@end


Now, I'm not going to explain the code because I already comment the code that I write, so enjoy!

// This is the code that you link to the ON button

- (IBAction)torchOn:(id)sender {


// Get all of the cameras that exists on the phone

NSArray *devices = [AVCaptureDevice devices];

AVCaptureDevice *flashDevice = nil;

for (id dev in devices) {

// Get the first camera device that has flash

if ([dev hasTorch]) {

flashDevice = dev;

break;

}

}

// Configure the camera to enable flash

[flashDevice lockForConfiguration:nil];

flashDevice.torchMode = AVCaptureTorchModeOn;

flashDevice.flashMode = AVCaptureFlashModeOn;

[flashDevice unlockForConfiguration];

// Instantiate the AVCaptureSession object and set up video recording

// but we don't really record to anything

session = [[AVCaptureSession alloc] init];

AVCaptureInput *inp = [[[AVCaptureDeviceInput alloc] initWithDevice:flashDevice error:nil] autorelease];

AVCaptureVideoDataOutput *vout = [[[AVCaptureVideoDataOutput alloc] init] autorelease];

dispatch_queue_t queue;

queue = dispatch_queue_create("com.example.MyQueue", NULL);

[vout setSampleBufferDelegate:nil queue:queue];

[session addInput:inp];

[session addOutput:vout];

// Start the camera -- start the flash

[session startRunning];

[devices release];

}


// This is the code that you link to the OFF button

- (IBAction)torchOff:(id)sender {

// Stop the camera -- stop the flash

[session stopRunning];

[session release];

}


You will not be able to compile this if you set the target to the Simulator. You have to set the target to the device.

Now you can enjoy your flash light app!

Comments

  1. now sell it nd get rich

    ReplyDelete
  2. Objective C is quite possibly the ugliest language ever.

    ReplyDelete
  3. Hi

    I tried it, but don't manage to make it work.

    I put the first code in

    main.m

    and the rest ?

    I created a View Controller with IB. But what should I link it to?

    thanks
    Sebastian

    ReplyDelete

Post a Comment

Popular posts from this blog

The Interviews

Visibility in Objective-C