拍照与从相册取照片(重新捋一遍逻辑)

之前写过, 觉得有些逻辑不是很妥当, 重写一篇

引入这个是为了访问相册, 存储拍照后的图片

#import 

签协议

@interface ViewController ()

声明属性

@property (nonatomic, strong) UIImagePickerController *imagePickerController;

先说用户选择拍照的情况, 涉及判断用户设备是否有相机, 是否允许使用相机, imagePickerController属性可以写懒加载, 因为都是取单张照片, 没必要每次都开辟空间, 但如果是取多张照片, 建议不要这么玩, 前面说过不再说

#pragma mark - 选择拍照
- (void)goTakePic:(UIButton *)button{

    //判断相机是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    
        //判断是否开启相机权限
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        //权限未开启
        if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
            [self userDidNotAllowCamera];
        }
    
        //权限已开启
        else{
    
            _imagePickerController = [[UIImagePickerController alloc] init];
            _imagePickerController.delegate = self;
            //模态推出照相机页面的样式
            _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            _imagePickerController.allowsEditing = YES;
            self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.imagePickerController animated:YES completion:^{}];
        }
    }
    //适用于没有相机的设备
    else{
        [self userDidnotHaveCamera];
    }
}

#pragma mark - 用户没有相机
- (void)userDidnotHaveCamera{

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"抱歉" message:@"相机不可用" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:nil];

//让alert自动消失
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(creatAlert:) userInfo:alertController repeats:NO];
}

#pragma mark 让警告消失
- (void)creatAlert:(NSTimer *)timer{

    UIAlertController *alert = [timer userInfo];
    [alert dismissViewControllerAnimated:YES completion:nil];
    alert = nil;
}

#pragma mark 用户没有允许相机权限
- (void)userDidNotAllowCamera{

    if ([UIDevice currentDevice].systemVersion.floatValue < 10){
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"抱歉" message:@"您尚未开启相机权限" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"去开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        //info plist中URL type中添加一个URL Schemes添加一个prefs值
        if([[UIApplication sharedApplication] canOpenURL:url]){
            //跳转到隐私
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
        }
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"不了" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:actionOK];
    [alertController addAction:actionCancel];
    [self presentViewController:alertController animated:YES completion:nil];
}
else{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"抱歉" message:@"您尚未开启相机权限" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:actionCancel];
    [self presentViewController:alertController animated:YES completion:nil];
}
}

================我是分割线=======================

接下来是选择去相册选择照片的情况, 其实和拍照差不多, 也就这句不一样,

self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

#pragma mark - 选择去相册
- (void)goToAlbum:(UIButton *)button{

    _imagePickerController = [[UIImagePickerController alloc] init];

    //去相册才会有效果
    _imagePicker.navigationBar.barTintColor = kRGBColor(0, 148, 255);
    //按钮字体颜色
    _imagePicker.navigationBar.tintColor = [UIColor whiteColor];
    //title字体大小和颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{
                                                           NSForegroundColorAttributeName:[UIColor whiteColor],
                                                           NSFontAttributeName:[UIFont systemFontOfSize:16 * SCALE_WIDTH]
                                                           }];

    //模态推出照相机页面的样式
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePickerController.delegate = self;
    //设置选择后的图片可被编辑
    self.imagePickerController.allowsEditing = YES;
    [self presentViewController:self.imagePickerController animated:YES completion:^{}];
}

====================分割线再次降临================
接下来是图片获取之后的协议方法, 拍照和相册共用这个协议方法, 所以加个判断, 如果是拍照才把拍后的图片存到相册, 从相册取就不用存了

注意, 可编辑要在协议方法里配合
[info objectForKey:UIImagePickerControllerEditedImage];
编辑后的图片才会生效

#pragma mark - ImagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //原图
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

    //这个本来是想用于拍照后编辑的, 发现并没有用
    //self.isFullScreen = NO;

    //用拍下来的照片赋值
    _imageViewOfPic.image = image;

    //只有选择拍照才会把照片保存到相册
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
    
        //访问相册权限, ios9之后的api, 要引入
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        //未开启相册权限
        //PHAuthorizationStatusNotDetermined,
        //PHAuthorizationStatusRestricted
        //PHAuthorizationStatusDenied
        if(status == PHAuthorizationStatusDenied){
        
            [self userDidnotAllowAlbum];
        }else{
            //存入本地相册
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }

    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 存储到系统相册结果回调
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{

    if (error){
        NSLog(@"%@", error);
    }
    else{
        NSLog(@"保存成功");
    }
}

#pragma mark 用户不允许使用相册
- (void)userDidnotAllowAlbum{

   if ([UIDevice currentDevice].systemVersion.floatValue < 10){
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您尚未开启相册权限" message:@"无法存入所拍的照片" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"去开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        //info plist中URL type中添加一个URL Schemes添加一个prefs值
        if([[UIApplication sharedApplication] canOpenURL:url]){
            //跳转到隐私
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"]];
        }
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"不了" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:actionOK];
    [alertController addAction:actionCancel];
    [self presentViewController:alertController animated:YES completion:nil];
}
else{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您尚未开启相册权限" message:@"无法存入所拍的照片" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:actionCancel];
    [self presentViewController:alertController animated:YES completion:nil];
}
}

你可能感兴趣的:(拍照与从相册取照片(重新捋一遍逻辑))