'AVFoundation.framework'에 해당되는 글 1건

  1. 2011.09.08 [Xcode/iOS] 아이폰 카메라 플래시 제어하기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


UCC 다바다를 찾아오신 분은 '여기'로 가주세요..



앱 스토어에 Flashlight 앱이 많이 있지요..

그런 프로그램들의 주요 기능!

아이폰 카메라 플래시 제어하는 부분을 살펴보겠습니다.


우선 가장 먼저 Framework을 추가해줘야 합니다.

프로젝트 Target 창을 여신 후 Build PhasesLink Binary With Libraries 항목의 + 를 눌러서
AVFoundation.framework을 프로젝트에 추가해줍니다.

Framework를 추가한 후 아래와 같이 변수를 추가해줍니다.
 

@interface TorchViewController : UIViewController

{

    AVCaptureSession* m_torchSession;

    AVCaptureDevice *device;

}


@property (nonatomic, retain) AVCaptureSession* m_torchSession;

@property (nonatomic, retain) AVCaptureDevice *device;


.m 파일에도 synthesize를 해주고..

@implementation TorchViewController


@synthesize m_torchSession;

@synthesize device;



view가 Load 된 후에 카메라가 달려있는지 체크를 한 다음 capture device를 생성해줍니다.

- (void)viewDidLoad

{

    [super viewDidLoad];

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerCameraDeviceFront] == YES)

    {

        self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        

        if ([device hasTorch] && [device hasFlash])

        {

            if (device.torchMode == AVCaptureTorchModeOff)

            {

                NSLog(@"It's currently off.. turning on now.");

                

                AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];

                AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

                

                AVCaptureSession *session = [[AVCaptureSession alloc] init];

                

                [session beginConfiguration];

                [device lockForConfiguration:nil];

                

                [device setTorchMode:AVCaptureTorchModeOn];

                [device setFlashMode:AVCaptureFlashModeOn];

                

                [session addInput:flashInput];

                [session addOutput:output];

                

                [device unlockForConfiguration];

                

                [output release];

                

                [session commitConfiguration];

                [session startRunning];

                

                self.m_torchSession = session;

                [session release];

            }

            else

            {

                NSLog(@"It's currently on.. turning off now.");

                

                self.m_torchSession = nil;

                [device lockForConfiguration:nil];

                

                device.torchMode = AVCaptureTorchModeOff;

                device.flashMode = AVCaptureFlashModeOff;

                [device unlockForConfiguration];

            }

        }

        else

        {

            UIAlertView* NoFlash = [[UIAlertView alloc] initWithTitle:@"Uh-Oh"

                                                              message:@"Your device doesn't have a flash camera"

                                                             delegate:nil

                                                    cancelButtonTitle:@"OK"

                                                    otherButtonTitles:nil];

            [NoFlash show];

            [NoFlash release];

        }

    }

}


화면에 라이트 켜기 버튼 등을 눌렀을 때 생성하지 않고 view가 load 됐을 때 생성하는 이유는 속도 때문입니다.
약 0.3~0.5초 정도 시간이 소요되더군요.
그래서 끄기/켜기에만 필요한 AVCaptureDevice와 AVCaptureSession 객체를 멤버로 가지고 있습니다.


그 후 버튼을 눌렀을 때 카메라 상태에 따라서 껐다/켰다를 해줍니다..

- (IBAction) OnButtonPower

{

    if ([device hasTorch] && [device hasFlash])

    {

        if (device.torchMode == AVCaptureTorchModeOff)

        {

            NSLog(@"It's currently off.. turning on now.");

            // 플래시 상태에 따라 버튼 이미지를 바꿔줌.

            [m_btnPower setImage:[UIImage imageNamed:@"power_button_on.jpeg"] forState:UIControlStateNormal];

            AVCaptureSession *session = m_torchSession;

            

            [session beginConfiguration];

            [device lockForConfiguration:nil];

            

            [device setTorchMode:AVCaptureTorchModeOn];

            [device setFlashMode:AVCaptureFlashModeOn];

            

            [device unlockForConfiguration];


            [session commitConfiguration];

            [session startRunning];

        }

        else

        {

            NSLog(@"It's currently on.. turning off now.");

             
// 플래시 상태에 따라 버튼 이미지를 바꿔줌.

            [m_btnPower setImage:[UIImage imageNamed:@"power_button_off.jpeg"] forState:UIControlStateNormal];


            [device lockForConfiguration:nil];

            

            device.torchMode = AVCaptureTorchModeOff;

            device.flashMode = AVCaptureFlashModeOff;

            [device unlockForConfiguration];

        }

    }

}






※ 퍼가실땐 출처를 밝혀주세요. (http://shkam.tistory.com/)


Posted by 고독한 프로그래머