Real Flashlight App for iPhone 4
- Import <avfoundation/avfoundation.h>
- Create an AVCaptureSession instance variable that we'll use
- Create two methods that will control the flash
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface FlashLightViewController : UIViewController {
AVCaptureSession *session;
}
- (IBAction)torchOn:(id)sender;
- (IBAction)torchOff:(id)sender;
@end
// 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];
}
now sell it nd get rich
ReplyDeleteObjective C is quite possibly the ugliest language ever.
ReplyDeleteHi
ReplyDeleteI 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