そこで、カスタム UIButton を作成してコードに追加し、interfacebuiler で接続を作成しました。ボタンをオンとオフのスイッチとして機能させたいのですが、どうすれば正しく機能しますか? 私はiPhone開発の初心者で、これは次の学期に有利なスタートを切るために夏に取っているこのクラスの学校のプロジェクト用です。
したがって、誰かがこれを正しい方法で行う方法を理解するのを手伝ってくれて、コードにコメントを書くことができれば. すべての助けをありがとう。デビッド H.
これが私のコードです:
//
// 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
ここに.mファイルがあります
//
// 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