//遵守协议
@interfaceViewController()
@property(nonatomic,strong)UIButton *userBtn;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//所有看得到的ui控件创建初始化方式都可以采用alloc initwithfram
self.userBtn = [[UIButtonalloc]initWithFrame:CGRectMake(30,60,80,80)];
//设置颜色
self.userBtn.backgroundColor= [UIColor redColor];
//设置圆形半径
self.userBtn.layer.cornerRadius=40;
self.userBtn.layer.masksToBounds=YES;
//添加点击事件:去访问系统相册
[self.userBtn addTarget:selfaction:@selector(setUserImage)forControlEvents:(UIControlEventTouchUpInside)];
//将按钮添加到屏幕上来
[self.view addSubview:self.userBtn];
}
//访问系统相册
-(void)setUserImage
{
//创建系统相册
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
//设置代理,到@interface后面遵守协议
UIImagePickerControllerDelegate>
imagePicker.delegate=self;
//弹出系统相册
[selfpresentViewController:imagePicker animated:YEScompletion:nil];
}
//这个方法是协议UIImagePickerControllerDelegate里面的,选择图片结束的时候就会自动调用
- (void)imagePickerController:(UIImagePickerController
*)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullableNSDictionary
*,id> *)editingInfo
{
//设置头像
[self.userBtnsetBackgroundImage:image forState:(UIControlStateNormal)];
//将系统相册消失掉
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
网友评论