iOS——调用系统相册相机

actionsheet的方法在8.3之后就被取代了,下面介绍目前比较新的方法:

1、info.plist文件写访问授权:

  1.  NSPhotoLibraryUsageDescription  
  2.     请求访问相册  
  3.     NSCameraUsageDescription  
  4.     请求访问相机  

更多可参考链接:iOS——info.plist访问权限的配置


2、这一步就不解释了,当用到uiimagepickercontroller的时候回提示警告的

3、这一步看个人习惯了

@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UIImagePickerController *imagePicker;
-(UIImageView *)imageView {
        if(!_imageView) {
            _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
            _imageView.center = self.view.center;
            _imageView.image = [UIImage imageNamed:@"bgImage.jpg"];
            _imageView.userInteractionEnabled = YES;  //这个一定要设置为YES,默认的为NO,NO的时候不可发生用户交互动作
            UITapGestureRecognizer *clickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];
            [_imageView addGestureRecognizer:clickTap];
        }
        return _imageView;
    }

4、实现调用系统相机相册的方法:

UIAlertController更多的使用方法可参考链接:iOS_UIAlertController的使用方法


-(void)chooseImage {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = YES;
        
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"从相机拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:self.imagePicker animated:YES completion:nil];
            }
        }];
        
        UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消");
        }];
        
        [actionSheet addAction:cameraAction];
        [actionSheet addAction:photoAction];
        [actionSheet addAction:cancelAction];
        
        [self presentViewController:actionSheet animated:YES completion:nil];
    }
    
//获取选择的图片
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        [picker dismissViewControllerAnimated:YES completion:nil];
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        self.imageView.image = image;
    }
    
//从相机或者相册界面弹出
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }

你可能感兴趣的:(ios)