简单相机

//只是个人写的相机,可供初学者参考
@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
}

   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值