[obj-c] simple Camera

iPhone 2012. 11. 10. 16:52


1. Project 생성

Single View Application



2. Lib 추가

MobileCoreServices.framework



3. UIViewController(기본적으로 생성되어 있던) UI 구성

사진을 표시할 UIImageView *imageView

카메라로 연결할 버튼 : UIButton *cameraBtn

앨범으로 연결할 버튼 : UIButton *albumBtn


4. 델리게이트

UIImagePickerControllerDelegate

UINavigationControllerDelegate


기본 UIViewController에 추가 


.h >

#import <MobileCoreServices/MobileCoreServices.h>

@interface XXXXXXViewController : UIViewController

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property (assign, nonatomic) BOOL newMedia; 

@property (strong, nonatomic) IBOutlet UIImageView *imageView    <- GUI의 UIImageVIew와 연결


.m > 

@synthesize imageView;

@synthesize newMedia;


5. 버튼의 이벤트 핸들러


이벤트 핸들러를 작성하고, 

GUI의 버튼과 이벤트 핸들러를 연결

.m에서


// 카메라 버튼을 눌렀을 때의 이벤트 핸들러

- (IBAction)useCamera:(id)sender {


    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){

        

        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

        

        imagePicker.delegate = self;

        

        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;


        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];


        imagePicker.allowsEditing = NO;

        [self presentModalViewController:imagePicker animated:YES];

        newMedia = YES;

        

    }

    

}


//Album버튼을 눌렀을 때의 이벤트 핸들러

- (IBAction)useCameraRoll:(id)sender {

    


    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]){

        

        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

        imagePicker.delegate = self;

        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];

        imagePicker.allowsEditing = NO;

        [self presentModalViewController:imagePicker animated:YES];

        newMedia = NO;

        

        

        

    }

     

}





.m에서


- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    [self dismissModalViewControllerAnimated:YES];

    if([mediaType isEqualToString:(NSString *)kUTTypeImage]){

        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

        

        imageView.image = image;

        if (newMedia) UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:finishedSavingWithError:contextInfo:), nil);

    }else if([mediaType isEqualToString:kUTTypeMovie]){

        

        // for movie

    }

    

    

}


- (void) image:(UIImage *) image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    if(error){

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Save failed" message:@"Failed to save image" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alert show];

    }

}


- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [self dismissModalViewControllerAnimated:YES];

}










Posted by tenn
,