2011년 5월 26일 목요일

iphone imagePicker 사용하여 이미지 버튼만들기

//
//  SampleDay9_1ViewController.h
//  SampleDay9-1
//
//  Created by bit on 11. 5. 26..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import

@interface SampleDay9_1ViewController : UIViewController {

IBOutlet UIButton *pictureButton;
}

@property (nonatomic, retain ) IBOutlet UIButton *pictureButton;

-(IBAction) onPhotoClicked:(id)sender ;

-(void)showImagePickerWithSourceType:(UIImagePickerControllerSourceType)sourceType ;
- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info;



@end




//
//  SampleDay9_1ViewController.m
//  SampleDay9-1
//
//  Created by bit on 11. 5. 26..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SampleDay9_1ViewController.h"

@implementation SampleDay9_1ViewController

@synthesize pictureButton;



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[pictureButton release];
[super dealloc];
}


-(IBAction) onPhotoClicked:(id)sender 
{
UIActionSheet *asheet;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
asheet = [[UIActionSheet alloc] initWithTitle:@"Select Data Source" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo Album", @"Camera", nil ];
}
else {
asheet = [[UIActionSheet alloc] initWithTitle:@"Select Data Source" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo Album", nil ];
}
[asheet showInView:self.view];
[asheet release];
}


#pragma mark UICationSheet Delegate

-(void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if( buttonIndex == [actionSheet cancelButtonIndex]) return;
if(buttonIndex == 0 ) {
[self showImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
else if ( buttonIndex == 1 ) {
[self showImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera];
}
}


#pragma mark Image Picker Loading

-(void)showImagePickerWithSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease ];
picker.sourceType = sourceType;
picker.allowsEditing = (sourceType == UIImagePickerControllerSourceTypeCamera );
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}

#pragma mark UIImagePickerController delegate

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

UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[pictureButton setImage:image forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}

@end

댓글 없음:

댓글 쓰기

참고: 블로그의 회원만 댓글을 작성할 수 있습니다.