UI_UIImagePickerController(读取图片)

创建图片

#pragma mark - 创建 photoImageView
- (void)createphotoImageView
{

    self.photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 70, 320, 390)];
    self.photoImageView.backgroundColor = [UIColor magentaColor];

    // 打开交互 实现手势轻拍
    self.photoImageView.userInteractionEnabled = YES;

    [self addSubview:self.photoImageView];
}

实现代理(3个)

@interface RootViewController () <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
// 绑定手势
- (void)viewDidLoad {
    [super viewDidLoad];

    // 轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];

    [self.rootView.photoImageView addGestureRecognizer:tapGR];

}
#pragma mark - 实现手势操作
- (void)tapGRAction:(UITapGestureRecognizer *)sender
{
    NSLog(@"tap it");

    // 底部弹出的控件
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选取",@"拍照", nil];

    // 在什么地方展示
    [sheet showInView:self.rootView];
}
#pragma mark - 轻拍手势中 action 代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld", buttonIndex);

    // 从相册选取或拍照
    if (actionSheet.firstOtherButtonIndex == buttonIndex) {
        NSLog(@"相册");

        // 相册是否可用
        if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypePhotoLibrary)]) {

            UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];

            // 通过代理方法拿到图片
            imagePickerVC.delegate = self;
            // 可以对图片进行编辑(对应代理方法)
            imagePickerVC.allowsEditing = YES;

            // 指定 pickVC 从相册选取
            imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            // 模态推出
            [self presentViewController:imagePickerVC animated:YES completion:nil];
        }

    } else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1)
    {
        NSLog(@"拍照");

        if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {

            UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];

            // 通过代理方法拿到图片
            imagePickerVC.delegate = self;
            // 可以对图片进行编辑(对应代理方法)
            imagePickerVC.allowsEditing = YES;

            // 指定 pickVC 从相机选取
            imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;

            // 模态推出
            [self presentViewController:imagePickerVC animated:YES completion:nil];
        }
    }
}
#pragma mark - 实现代理方法(拿到图片)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];

    UIImage *image =  [info objectForKey:UIImagePickerControllerEditedImage];

    self.rootView.photoImageView.image = image;
}
#pragma mark - 取消按钮的代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    // 一般在 rightButton
    [picker dismissViewControllerAnimated:YES completion:nil];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值