// ViewController.m
// 访问系统相册
//
// Created by lanouhn on 16/7/12.
// Copyright © 2016年 lanouhn. All rights reserved.
//
#import "ViewController.h"
//遵守协议
@interface ViewController ()
@property(nonatomic,strong)UIButton *userbtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 所有的能看得到的UI控件创建初始化呢方式都可以采用alloc initWithFrame
self.userbtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 60, 80, 80)];
// 设置颜色
self.userbtn.backgroundColor = [UIColor redColor];
// 设置圆形半径
self.userbtn.layer.cornerRadius = 40;
self.userbtn.layer.masksToBounds = YES;
// 添加点击事件:去访问系统相册
[self.userbtn addTarget:self action:@selector(setUserImage) forControlEvents:(UIControlEventTouchUpInside)];
// 将按钮添加到屏幕上来
[self.view addSubview:self.userbtn];
}
//访问系统相册
-(void)setUserImage
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//设置代理,到@interface后面遵守协议
imagePicker.delegate = self;
//弹出系统相册
[self presentViewController:imagePicker animated:YES completion:nil];
}
//这个方法是协议UIImagePickerControllerDelegate里面的,选择图片结束的时候就会自动调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary *)editingInfo
{
// 设置头像
[self.userbtn setBackgroundImage:image forState:(UIControlStateNormal)];
// 将系统相册消失掉
[picker
dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.