- 카메라를 통한 사진 선택
- 롤 이미지에서 사진 선택
- 도큐먼트에 이미지 저장
- 새로 실행할때 도큐먼트 이미지 로드 기능
-h 코드
//
// DocumentTestViewController.h
// DocumentTest
//
// Created by 병욱 손 on 11. 5. 1..
// Copyright 2011 GsiSystem. All rights reserved.
//
#import <UIKit/UIKit.h>
#define kFilename @"data.plist"
#define kImageFilename @"Test.png"
@interface DocumentTestViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
IBOutlet UIImageView *imageView;
IBOutlet UIButton *takePictureButton;
IBOutlet UIButton *selectFromCameraRollButton;
IBOutlet UITextField *field1;
IBOutlet UITextField *field2;
IBOutlet UITextField *field3;
IBOutlet UITextField *field4;
}
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIButton *takePictureButton;
@property (nonatomic, retain) UIButton *selectFromCameraRollButton;
@property (nonatomic, retain) UITextField *field1;
@property (nonatomic, retain) UITextField *field2;
@property (nonatomic, retain) UITextField *field3;
@property (nonatomic, retain) UITextField *field4;
- (NSString *)dataFilePath;
- (NSString *)dataImageFilePath;
- (void) applicationWillTerminate:(NSNotification *)notification;
- (IBAction)getCameraPicture:(id)sender;
- (IBAction)selectExistingPicture;
@end
- m 코드
//
// DocumentTestViewController.m
// DocumentTest
//
// Created by 병욱 손 on 11. 5. 1..
// Copyright 2011 GsiSystem. All rights reserved.
//
#import "DocumentTestViewController.h"
@implementation DocumentTestViewController
@synthesize field1, field2, field3, field4;
@synthesize imageView;
@synthesize takePictureButton;
@synthesize selectFromCameraRollButton;
/*
// 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];
//
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
field1.text = [array objectAtIndex:0];
field2.text = [array objectAtIndex:1];
field3.text = [array objectAtIndex:2];
field4.text = [array objectAtIndex:3];
[array release];
}
//
NSString *imageFilePath = [self dataImageFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:imageFilePath]) {
UIImage *image = [UIImage imageWithContentsOfFile:imageFilePath];
imageView.image = image;
}
//
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
[super viewDidLoad];
//
if (![UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
takePictureButton.hidden = YES;
selectFromCameraRollButton.hidden = YES;
}
}
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (NSString *)dataImageFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kImageFilename];
}
- (void) applicationWillTerminate:(NSNotification *)notification
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:field1.text];
[array addObject:field2.text];
[array addObject:field3.text];
[array addObject:field4.text];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}
/*
// 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 {
[super dealloc];
[field1 release];
[field2 release];
[field3 release];
[field4 release];
[imageView release];
[takePictureButton release];
[selectFromCameraRollButton release];
}
- (IBAction)getCameraPicture:(id)sender
{
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = (sender == takePictureButton) ?
UIImagePickerControllerSourceTypeCamera :
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (IBAction)selectExistingPicture
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library"
message:@"Device does not support a photo library"
delegate:nil
cancelButtonTitle:@"Drat!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
imageView.image = image;
printf("Image:%f, %f", image.size.width, image.size.height);
[UIImagePNGRepresentation(image) writeToFile:[self dataImageFilePath]
atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
@end
- 인터페이스 빌더 내용
캡춰 맥에서 어케 하는지 몰겠군요. ^^
출처 : http://iamgsi.tistory.com/542
'프로그래밍 > iOS' 카테고리의 다른 글
아이폰 개발 소스모음 (0) | 2011.05.21 |
---|---|
iphone에서 http 사용하기 (get/post) - 소스코드는 퍼왔습니다. (0) | 2011.05.21 |
화면의 가로보기/세로보기 설정 방법 (0) | 2011.05.20 |
전화 통화 후 다시 돌아오기 (0) | 2011.05.20 |
자동회전과 자동크기 조절 (0) | 2011.05.20 |