// ?ViewController.m
// ?訪問系統(tǒng)相冊
//
// ?Created by lanouhn on 16/7/12.
// ?Copyright ? 2016年 lanouhn. All rights reserved.
//
#import "ViewController.h"
//遵守協(xié)議
@interface ViewController ()
@property(nonatomic,strong)UIButton *userbtn;
@end
@implementation ViewController
- (void)viewDidLoad {
? ?[super viewDidLoad];
// ?所有的能看得到的UI控件創(chuàng)建初始化呢方式都可以采用alloc initWithFrame
? ?self.userbtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 60, 80, 80)];
// ?設(shè)置顏色
? ?self.userbtn.backgroundColor = [UIColor redColor];
// ?設(shè)置圓形半徑
? ?self.userbtn.layer.cornerRadius = 40;
? ?self.userbtn.layer.masksToBounds = YES;
// ?添加點擊事件:去訪問系統(tǒng)相冊
? ?[self.userbtn addTarget:self action:@selector(setUserImage) forControlEvents:(UIControlEventTouchUpInside)];
// ?將按鈕添加到屏幕上來
? ?[self.view addSubview:self.userbtn];
? ?}
//訪問系統(tǒng)相冊
-(void)setUserImage
{
? ?UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//設(shè)置代理,到@interface后面遵守協(xié)議
? ?imagePicker.delegate = self;
//彈出系統(tǒng)相冊
? ?[self presentViewController:imagePicker animated:YES completion:nil];
}
//這個方法是協(xié)議UIImagePickerControllerDelegate里面的氏堤,選擇圖片結(jié)束的時候就會自動調(diào)用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary *)editingInfo
{
// ?設(shè)置頭像
? ?[self.userbtn setBackgroundImage:image forState:(UIControlStateNormal)];
// ?將系統(tǒng)相冊消失掉
? ?[picker
? ? dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
? ?[super didReceiveMemoryWarning];
? ?// Dispose of any resources that can be recreated.