iOS -- UIImagePickerController

UIImagePickerController是系统提供的用来获取图片或视频的接口,使用UIImagePickerController类来获取图片的基本步骤如下:

  1. 初始化UIImagePickerController类
  2. 设置UIImagePickerController实例的数据来源
  3. 设置UIImagePickerController实例的代理
  4. 设置是否允许编辑图片,若允许则allowsEditing属性值置为YES
  5.  设置完UIImagePickerController实例的属性之后,在需要获取图片时要跳转到图像选取控制器当中去选取或拍摄图片
  6. 完成图片的选取后回调代理方法

UIImagePickerController数据来源:

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
    UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库
    UIImagePickerControllerSourceTypeCamera, // 来自相机
    UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册
};

 

遵循协议

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

声明属性

@property (nonatomic,strong) UIImagePickerController * pickerController;

懒加载

- (UIImagePickerController *)pickerController{
    if (!_pickerController) {
        _pickerController = [[UIImagePickerController alloc] init];
        _pickerController.delegate = self;
        _pickerController.allowsEditing = YES;
    }
    return _pickerController;
}

代理

#pragma mark - UIImagePickerControllerDelegate
//完成图片的选取后回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //选取完图片后跳转回原控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    //从info中将图片取出,并加载到imageview当中
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.headerImageView.image = image;
    
    // 创建保存图像时需要传入的选择器对象(回调方法格式固定)
    SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
    // 将图像保存到相册(第三个参数需要传入上面格式的选择器对象)
    UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, NULL);
}

//取消选取调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//保存图片到相册后,回调的相关方法,查看是否保存成功
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error == nil){
        NSLog(@"Image was saved successfully.");
    } else {
        NSLog(@"An error happened while saving the image.");
        NSLog(@"Error = %@", error);
    }
}

 

调用前

#pragma mark - 相册

/**
 *   是否支持相册
 *
 *  @return YES 是支持  NO是不支持
 */
- (BOOL)isAvailableAlbum
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        return YES;
    }
    return NO;
}
/**
 *   是否支持拍照
 *
 *  @return YES 是支持  NO是不支持
 */
- (BOOL)isAvailabletakePhoto
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera ]) {
        return YES;
    }
    return NO;
}

/**
 *  从相册选择
 */
- (void)selectImageFromAlbum
{
    if([self isAvailableAlbum] == YES)
    {
        self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.pickerController animated:YES completion:nil];
    }
}
/**
 *   拍照
 */
- (void)selectImageFromTakePhoto
{
    if([self isAvailabletakePhoto] == YES)
    {
        self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:self.pickerController animated:YES completion:nil];
    }
}

 

 

调用

//头像
- (IBAction)headerImageTapAction:(UITapGestureRecognizer *)sender {
    
    
    _alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * photo = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self selectImageFromTakePhoto];
    }];
    
    UIAlertAction * library = [UIAlertAction actionWithTitle:@"我的相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self selectImageFromAlbum];
    }];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [self.alertController addAction:photo];
    [self.alertController addAction:library];
    [self.alertController addAction:cancelAction];
    
    [self presentViewController:self.alertController animated:YES completion:nil];
    
    [self presentViewController:self.pickerController animated:YES completion:^{
        
    }];
    
}

 

posted @ 2016-06-12 15:09  顺手给我new一个对象  阅读(1207)  评论(0编辑  收藏  举报