iOS 访问相机 相册

@interface MainViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>


@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    [self.view addSubview:button];

    button.frame = CGRectMake(20, 100, self.view.bounds.size.width-40, 40);

    [button setTitle:@"相册图片" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];

    [self.view addSubview:button1];

    button1.frame = CGRectMake(20, 200, self.view.bounds.size.width-40, 40);

    [button1 setTitle:@"相机图片" forState:UIControlStateNormal];

    [button1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];

    

}



- (void)click:(UIButton *)but

{

    

    [self pickImageFromAlbum];

   

}



- (void)click1:(UIButton *)but

{

    

    

    [self pickImageFromCamera];

    

    

}



//相册

- (void)pickImageFromAlbum

{

    UIImagePickerController *imagepicker = [[UIImagePickerController alloc]init];

    imagepicker.delegate = self;

    imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    

    imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    

    imagepicker.allowsEditing = YES;

    [self presentViewController:imagepicker animated:YES completion:^{

    }];

}


//摄像头


- (void)pickImageFromCamera

{

      UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];

    imagePicker.delegate = self;

    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    imagePicker.allowsEditing  = YES;

    

    [self presentViewController:imagePicker animated:YES completion:^{

    }];

}



//选完照片的回调


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info

{

    //检测是不是原始照片

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    //检测是不是编辑过的照片

    UIImage *image1 = [info objectForKey:UIImagePickerControllerEditedImage];

    

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        

        

        

    }

    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {

        

        

    }

    [self dismissViewControllerAnimated:YES completion:^{

        

        

        

    }];

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    

    

    [picker dismissViewControllerAnimated:YES completion:^{

        

    }];

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值