@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];
}
UIAlertController的使用+唤醒相机|相册的使用
最新推荐文章于 2021-01-12 07:30:23 发布