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:^{
        
    }];
    
}

 

转载于:https://www.cnblogs.com/HOYF/p/5577626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值