#import"ViewController.h"
@interfaceViewController()
@property(strong,nonatomic)IBOutletUIImageView*myHeadPortrait;
@end
@implementationViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// 调用setHeadPortrait方法
[selfsetHeadPortrait];
}
#pragma mark - 设置头像-(void)setHeadPortrait { // // 把头像设置成圆形// self.iconImg.layer.cornerRadius=self.iconImg.frame.size.width/2;// self.iconImg.layer.masksToBounds=YES; // 给头像加一个圆形边框 self.iconImg.layer.borderWidth = 1.5f; self.iconImg.layer.borderColor = [UIColor whiteColor].CGColor; /** * 添加手势:也就是当用户点击头像了之后,对这个操作进行反应 */ //允许用户交互 _iconImg.userInteractionEnabled = YES; //初始化一个手势 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alterHeadPortrait:)]; //给imageView添加手势 [_iconImg addGestureRecognizer:singleTap]; }// 方法:alterHeadPortrait-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{ /** * 弹出提示框 */ //初始化提示框 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; //按钮:从相册选择,类型:UIAlertActionStyleDefault [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //初始化UIImagePickerController UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init]; //获取方式1:通过相册(呈现全部相册),UIImagePickerControllerSourceTypePhotoLibrary //获取方式2,通过相机,UIImagePickerControllerSourceTypeCamera //获取方法3,通过相册(呈现全部图片),UIImagePickerControllerSourceTypeSavedPhotosAlbum PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //允许编辑,即放大裁剪 PickerImage.allowsEditing = YES; //自代理 PickerImage.delegate = self; //页面跳转 [self presentViewController:PickerImage animated:YES completion:nil]; }]]; //按钮:拍照,类型:UIAlertActionStyleDefault [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ /** 其实和从相册选择一样,只是获取方式不同,前面是通过相册,而现在,我们要通过相机的方式 */ UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init]; //获取方式:通过相机 PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera; PickerImage.allowsEditing = YES; PickerImage.delegate = self; [self presentViewController:PickerImage animated:YES completion:nil]; }]]; //按钮:取消,类型:UIAlertActionStyleCancel [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert animated:YES completion:nil];}//PickerImage完成后的代理方法- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
//定义一个newPhoto,用来存放我们选择的图片。
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
self.iconImg.image = newPhoto;
[self dismissViewControllerAnimated:YES completion:nil];
}
空格自己分开下。。。。xcode复制过来就这样了
大致流程
一般在APP中,修改头像是最基本的功能之一了。一般是两种方式的修改:从相册选择图片或者拍照。那么这里就来讲一下如何具体实现这个功能。
Step1:点击头像 ->手势(UITapGestureRecognizer)
QQ的更换头像操作
首先,点击头像。因为头像是直接放在ImageView中的,默认情况下当我们点击头像的时候,头像是不会有任何反应的。因此,我们需要给头像的ImageView添加一个点击事件,方法如下:
/**
* 添加手势:也就是当用户点击头像了之后,对这个操作进行反应
*///允许用户交互_myHeadPortrait.userInteractionEnabled=YES;//初始化一个手势UITapGestureRecognizer*singleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(alterHeadPortrait:)];//给ImageView添加手势[_myHeadPortrait addGestureRecognizer:singleTap]; }
Step2:弹出选择提示->提示框(UIAlertController)
通过添加UITapGestureRecognizer(手势),系统就知道了我点击了头像,接着,就可以添加具体的方法来进行操作了。在上一步,我为这个手势的action,selector(选择)了一个方法来执行,即alterHeadPortrait:(注意有冒号的),也就是当我们点击了头像之后,会执行alterHeadPortrait:这个方法:
// 方法:alterHeadPortrait-(void)alterHeadPortrait:(UITapGestureRecognizer*)gesture{/**
* 弹出提示框
*///初始化提示框UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];//按钮:从相册选择,类型:UIAlertActionStyleDefault[alert addAction:[UIAlertActionactionWithTitle:@"从相册选择"style:UIAlertActionStyleDefaulthandler:nil]];//按钮:拍照,类型:UIAlertActionStyleDefault[alert addAction:[UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDefaulthandler:nil]];//按钮:取消,类型:UIAlertActionStyleCancel[alert addAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil]]; [selfpresentViewController:alert animated:YEScompletion:nil];}
通过UIAlertController(提示框)这个类,我们创建好了一个提示框,如下:
点击头像,弹出提示框
现在,当我们点击取消(或者点击按钮以外的区域)提示框就会被自动取消掉,并将提示框收起来。
Step3:从相册选择或者拍照选择头像->UIImagePickerController
好了,绕了这么久,终于开始进入主题了,即选择图片或者拍照了。那么现在该肿么办呢?好像毫无头绪的样子。。。
这里就需要通过UIImagePickerController,通过它,我们就可以让我们的APP轻松的实现访问相册或者拍照:
操作UIImagePickerController,需要实现两个协议:
进行相册图片选择或者相机拍照的实现代码如下:
//初始化UIImagePickerControllerUIImagePickerController*PickerImage = [[UIImagePickerControlleralloc]init];//获取方式1:通过相册(呈现全部相册),UIImagePickerControllerSourceTypePhotoLibrary//获取方式2,通过相机,UIImagePickerControllerSourceTypeCamera//获取方方式3,通过相册(呈现全部图片),UIImagePickerControllerSourceTypeSavedPhotosAlbumPickerImage.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//方式1//允许编辑,即放大裁剪PickerImage.allowsEditing=YES;//自代理PickerImage.delegate=self;//页面跳转[selfpresentViewController:PickerImage animated:YEScompletion:nil];
运行效果如图: