UIAlertController的使用+唤醒相机|相册的使用

@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView *chooseView;
@property (nonatomic, strong) UIButton *openCameraBtn;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor darkGrayColor];
    //加载从相册中选择的图片
    self.chooseView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    self.chooseView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.chooseView];
    
    //开启系统相机|相册
    self.openCameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.openCameraBtn.frame = CGRectMake(100, 350, 200, 40);
    [self.openCameraBtn setTitle:@"开启系统相机|相册" forState:UIControlStateNormal];
    [self.openCameraBtn setBackgroundColor:[UIColor orangeColor]];
    [self.openCameraBtn addTarget:self action:@selector(openSystemCamera) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.openCameraBtn];
}

/*
 UIImagePickerController(图片拾取器)
 能够根据不同的额指令,选择开启相机或者系统相册
 
 ps:使用图片拾取器的时候,都别忘了实现两个协议
 UIImagePickerControllerDelegate,
 UINavigationControllerDelegate
 
 pss:模拟器和某些设备不支持开启相机,所以在开启相机功能执行之前,有必要判断当前设备是否存在相机
 */

- (void)openSystemCamera{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"开启系统相机|相册" preferredStyle:UIAlertControllerStyleActionSheet];// UIAlertControllerStyleAlert(警告框)

//Ps:UIAlertControllerStyleAlert(警告框)还可以添加TextField,UIAlertControllerStyleActionSheet不可以`;
/*添加文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.textColor = [UIColor redColor];
        //给文本框设置监听(这是这里面的特殊用法,一般情况下textField是不能设置监听的)
        [textField addTarget:self action:@selector(doSomething:) forControlEvents:UIControlEventEditingChanged];
    }];
*/
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        //创建图片拾取器
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        //设置代理
        imagePicker.delegate = self;
        //设置图片拾取器类型(相机还是相册)
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        //是否允许图片编辑
        imagePicker.allowsEditing = YES;
        //唤醒相机
        [self presentViewController:imagePicker animated:YES completion:nil];
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"系统相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }]];
    
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

#pragma maker -
#pragma mark - 回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //完成获取图片的操作
//    NSLog(@"%@",info);
    
    UIImage *chooseImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    self.chooseView.image = chooseImage;
    
    //将图片从系统相册当中取出,并存到应用的沙盒中
    NSString *homPath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents"];
    //路径
    NSString *realPath = [homPath stringByAppendingPathComponent:[NSString stringWithFormat:@"00%d.image",arc4random()%10000]];
    
    //图片存入沙盒
    [UIImageJPEGRepresentation(chooseImage, 1.0f) writeToFile:realPath atomically:YES];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值