//遵守協(xié)議
@interfaceViewController()
@property(nonatomic,strong)UIButton *userBtn;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//所有看得到的ui控件創(chuàng)建初始化方式都可以采用alloc initwithfram
self.userBtn = [[UIButtonalloc]initWithFrame:CGRectMake(30,60,80,80)];
//設(shè)置顏色
self.userBtn.backgroundColor= [UIColor redColor];
//設(shè)置圓形半徑
self.userBtn.layer.cornerRadius=40;
self.userBtn.layer.masksToBounds=YES;
//添加點(diǎn)擊事件:去訪問(wèn)系統(tǒng)相冊(cè)
[self.userBtn addTarget:selfaction:@selector(setUserImage)forControlEvents:(UIControlEventTouchUpInside)];
//將按鈕添加到屏幕上來(lái)
[self.view addSubview:self.userBtn];
}
//訪問(wèn)系統(tǒng)相冊(cè)
-(void)setUserImage
{
//創(chuàng)建系統(tǒng)相冊(cè)
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
//設(shè)置代理,到@interface后面遵守協(xié)議
UIImagePickerControllerDelegate>
imagePicker.delegate=self;
//彈出系統(tǒng)相冊(cè)
[selfpresentViewController:imagePicker animated:YEScompletion:nil];
}
//這個(gè)方法是協(xié)議UIImagePickerControllerDelegate里面的,選擇圖片結(jié)束的時(shí)候就會(huì)自動(dòng)調(diào)用
- (void)imagePickerController:(UIImagePickerController
*)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullableNSDictionary
*,id> *)editingInfo
{
//設(shè)置頭像
[self.userBtnsetBackgroundImage:image forState:(UIControlStateNormal)];
//將系統(tǒng)相冊(cè)消失掉
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}