UIImagePickerController 用法

本文详细介绍了如何在iOS应用中利用UIImagePickerController实现用户通过相机拍摄或图片库选择功能来更换头像的功能。通过设置interactionEnabled属性为YES使imageView可交互,并通过UITapGestureRecognizer触发选择操作。当用户完成选择后,使用UIImagePickerControllerDelegate协议方法处理选择的图片并将其显示在用户头像上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//首选需要遵循协议 
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>


//如果是imageView的话, interactionEnabled必须设置为YES
    self.headImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondToTapGesture:)];
    [self.headImageView addGestureRecognizer:tapGesture];

//手势方法
-(void)respondToTapGesture:(UITapGestureRecognizer *)gestrue{

        UIImagePickerController * imageController = [[UIImagePickerController alloc]init];
        imageController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//数据源类型有三种
 ![UIImagePickerControllerSourceTypePhotoLibrary](http://img.blog.csdn.net/20160502162802766)

UIImagePickerControllerSourceTypeCamera
这种方式直接打开设备相机取图

![UIImagePickerControllerSourceTypeSavedPhotosAlbum
](http://img.blog.csdn.net/20160502163631270)

        imageController.allowsEditing = YES; //设置可以编辑
        imageController.delegate = self;//设定委托
        [self presentViewController:imageController animated:YES completion:nil];
}


//UIImagePickerControllerDelegate 协议方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    self.headImageView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值