//只是个人写的相机,可供初学者参考
@interface PickerViewController : UIViewController
@property(nonatomic,retain)UIImagePickerController * imagePicker;
@end
#import "PickerViewController.h"
@interface PickerViewController ()
@property(nonatomic,retain) UIImageView * imageView;
@end
@implementation PickerViewController
@synthesize imageView;
@synthesize imagePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 70, 300, 250)];
imageView.backgroundColor = [UIColor lightGrayColor];
UIButton * takePhoto = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takePhoto setTitle:@"功能" forState:UIControlStateNormal];
takePhoto.layer.borderWidth = 1;
takePhoto.layer.borderColor = [UIColor lightGrayColor].CGColor;
[takePhoto addTarget:self action:@selector(showAction:) forControlEvents:UIControlEventTouchUpInside];
takePhoto.frame = CGRectMake(250, 30, 50, 30);
[self.view addSubview:takePhoto];
[self.view addSubview:imageView];
}
-(void)showAction:(UIButton *)button
{
/*
UIActionSheet实例,显示简单菜单。
采用滑动方式呈现在屏幕上,等待用户响应
*/
//这里取消按钮的index一定是最后一个 此时为2 如果destructiveButtonTitle的值不是nil取消按钮的index为3
UIActionSheet * photoBtn = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
//[photoBtn setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[photoBtn showInView:[self.view window]];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[self takePhotoBtn];//调用照相方法
}else{
[self checkPhoto];//调用选择手机相册方法
}
}
-(void)takePhotoBtn
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]==YES) {//判断相机是否可用
imagePicker = [[[UIImagePickerController alloc] init]autorelease];
imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;//设置imgePicker的sourceType为相机类型
imagePicker.delegate = self;//设置代理
imagePicker.allowsEditing = YES;//设置照片可被编辑
[self presentViewController:imagePicker animated:YES completion:nil];//设置完以后进入照相界面
}else{//如果没有相机或者相机不可用 弹出提示
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"相机不能用" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void)checkPhoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]==YES) {
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
-(void)show:(UIImage *)image
{
self.imageView.image = image;
}
#pragma mark-
#pragma mark- UIImagePickerControllerDelegate Methods
//系统默认实现了 imagePickerControllerDidCancel: 这个方法,这里可以不写
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%@",info);
UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil);//将所拍照片保存到相册
imageView.image = picture; //将照片放到imageView上 显示出来
[picker dismissViewControllerAnimated:YES completion:nil];
// [self performSelector:@selector(show:) withObject:image afterDelay:0.5];//将所拍照片显示到imageView
}