#import
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIImageView *imgV;
- (IBAction)openCamera:(id)sender;
- (IBAction)openPhotoLibrary:(id)sender;
- (IBAction)openSavedPhotoAlbum:(id)sender;
@end
#import "ViewController.h"
@implementation ViewController
@synthesize imgV;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren‘t in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setImgV:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)openCamera:(id)sender
{
//UIImagePickerController 类方法,判断源是否可用 UIImagePickerController是一个图片挑选控制器 可以通过三种途径挑选图片。Camera、PhotoLibrary、SavedPhotoAlbum
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];//判断照相机是否可用(是否有摄像头)
if(hasCamera == YES)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = YES; //是否可编辑
[self presentModalViewController:picker animated:YES];
[picker release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"ok!" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%@",info);
// {
// UIImagePickerControllerCropRect = "NSRect: {{-1, 320}, {1937, 1937}}";
// UIImagePickerControllerEditedImage = "";
// UIImagePickerControllerMediaMetadata = {
// DPIHeight = 72;
// DPIWidth = 72;
// Orientation = 6;
// "{Exif}" = {
// ApertureValue = "2.970853654340484";
// BrightnessValue = "2.246098001053075";
// ColorSpace = 1;
// DateTimeDigitized = "2012:09:20 11:47:12";
// DateTimeOriginal = "2012:09:20 11:47:12";
// ExposureMode = 0;
// ExposureProgram = 2;
// ExposureTime = "0.06666666666666667";
// FNumber = "2.8";
// Flash = 24;
// FocalLength = "3.85";
// ISOSpeedRatings = (
// 125
// );
// MeteringMode = 5;
// PixelXDimension = 2592;
// PixelYDimension = 1936;
// SceneType = 1;
// SensingMethod = 2;
// Sharpness = 2;
// ShutterSpeedValue = "3.911199862602335";
// SubjectArea = (
// 1295,
// 967,
// 699,
// 696
// );
// WhiteBalance = 0;
// };
// "{TIFF}" = {
// DateTime = "2012:09:20 11:47:12";
// Make = Apple;
// Model = "iPhone 4";
// Software = "5.1.1";
// XResolution = 72;
// YResolution = 72;
// };
// };
// UIImagePickerControllerMediaType = "public.image";
// UIImagePickerControllerOriginalImage = "";
// }
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
imgV.image = image;
//如果想把某个UIImage对象存储到默认相册,使用下面代码
//UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[self dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)openPhotoLibrary:(id)sender //相册列表
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES; //是否可编辑
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (IBAction)openSavedPhotoAlbum:(id)sender //默认相册
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate = self;
picker.allowsEditing = YES; //是否可编辑
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)dealloc {
[imgV release];
[super dealloc];
}
@end