336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


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


아이폰 프로그래밍을 하다 보면 버전 정보를 가져와야 할 필요가 있는 경우가 있지요. 

아주 간단하게 현재 실행중인 IOS 버전을 가져오는 방법입니다.

NSString* systemVersion = [[UIDevice currentDevice] systemVersion];

NSLog(@"system version = %@", systemVersion); 


위 코드는 현재 디바이스의 IOS 버전 정보를 NSString* 형태로 리턴해줍니다. 
예) 4.3.2 

대부분의 경우 버전 2번째 자리까지 사용하는 경우가 많으니까 실수형태로 가져와서 버전 비교하는게 쉽습니다.

    float fVersion = [systemVersion floatValue];

    if(fVersion < 4.0)

    {

        // 4.0 이전 버전인 경우.

    }

    else if(fVersion <= 4.2)

    {

        // 4.0 ~ 4.2 버전인 경우.

    } 



세부 버전 정보까지 관리하려면 문자열을 배열로 만들어서 비교해도 됩니다.

    NSArray* arrVersions = [systemVersion componentsSeparatedByString:@"."];


    if([arrVersions count] == 3)

    {

        NSLog(@"%@, %@, %@", [arrVersions objectAtIndex:0], [arrVersions objectAtIndex:1], [arrVersions objectAtIndex:2]);

    }




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





Posted by 고독한 프로그래머
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.




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


아이폰 앱 파일이 있는 폴더에는 하위폴더가 3개 있습니다.
Documents, Library, tmp 폴더죠.


NSSearchPathForDirectoriesInDomains 함수를 이용하면 위 폴더를 포함한 전체 경로를 가져올 수 있습니다.
 

- Document 폴더 전체 경로 가져오기.

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


    NSString* documentDir = [paths objectAtIndex:0];

    

    NSLog(@"%@", documentDir);


- Library 폴더 전체 경로 가져오기.

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

    

    NSString* libraryDir = [paths objectAtIndex:0];

    

    NSLog(@"%@", libraryDir);




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



Posted by 고독한 프로그래머
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 고독한 프로그래머