调用系统的相册、相机、闪光灯,可以选择相册的照片

#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];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值