#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageview;
@property (nonatomic, strong)UIImagePickerController *pickerVC;
@property(nonatomic, strong)AVCaptureDevice *device;
@end
@implementation ViewController
- (IBAction)pickPhoto:(id)sender {
[self setAlertVC];
}
- (IBAction)closeshared:(id)sender {
// 如果闪光灯已经打开,那么把闪光灯关闭
[self.device setTorchMode:AVCaptureTorchModeOff];
// 解除对设备硬件的独占
[self.device unlockForConfiguration];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.pickerVC = [[UIImagePickerController alloc]init];
self.pickerVC.delegate = self;
self.pickerVC.allowsEditing = YES;
// self.pickerVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
}
- (void)setAlertVC{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择头像" message:@"本地相册或者拍照" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self goPhotoLibrary];
}];
UIAlertAction *camaraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self goCamara];
}];
UIAlertAction *torchAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self goTorch];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
nil;
}];
[alertVC addAction:photoAction];
[alertVC addAction:camaraAction];
[alertVC addAction:torchAction];
[alertVC addAction:cancelAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
- (void)goPhotoLibrary{
if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypePhotoLibrary)])
{
self.pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.pickerVC animated:YES completion:nil];
}else{
NSLog(@"没相册");
}
}
- (void)goCamara{
if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
self.pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.pickerVC animated:YES completion:nil];
}else{
UIAlertController *altvc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"不存在相机" style:UIAlertActionStyleCancel handler:nil];
[altvc addAction:action];
[self presentViewController:altvc animated:YES completion:nil];
}
}
- (void)goTorch{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// 返回用于捕获视频数据的设备(摄像头)(必要的步骤)
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//是否有闪光灯
if ([self.device hasTorch]) {
// 请求独占设备的硬件性能
[self.device lockForConfiguration:nil];
//闪光灯关闭状态
if (self.device.torchMode == AVCaptureTorchModeOff){
// 打开闪光灯
[self.device setTorchMode: AVCaptureTorchModeOn];
}else{
// 如果闪光灯已经打开,那么把闪光灯关闭
[self.device setTorchMode:AVCaptureTorchModeOff];
// 解除对设备硬件的独占
[self.device unlockForConfiguration];
}
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"%@",info);
self.imageview.image = info[@"UIImagePickerControllerEditedImage"];
[self.pickerVC dismissViewControllerAnimated:YES completion:nil];
}