美文网首页
ios.访问相册(设置头像)

ios.访问相册(设置头像)

作者: 大大大西瓜 | 来源:发表于2016-07-12 23:20 被阅读0次

#import"ViewController.h"

//遵守协议

@interfaceViewController()

@property(nonatomic,strong)UIButton*userbtn;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

//所有的能看得到的ui控件创建初始化方式都可以采用allocinitWithFrame

self.userbtn=[[UIButtonalloc]initWithFrame:CGRectMake(160,60,80,80)];

//设置颜色

self.userbtn.backgroundColor=[UIColorredColor];

//设置圆形半径

self.userbtn.layer.cornerRadius=40;

self.userbtn.layer.masksToBounds=YES;

//添加点击事件:去访问系统相册

[self.userbtnaddTarget:selfaction:@selector(setUserImage)forControlEvents:(UIControlEventTouchUpInside)];

//将按钮添加到屏幕上面来

[self.viewaddSubview:self.userbtn];

}

//访问系统相册

-(void)setUserImage

{

UIImagePickerController*imagePicker=[[UIImagePickerControlleralloc]init];

//设置代理,到@interface后面遵守协议

UIImagePickerControllerDelegate>

imagePicker.delegate=self;

[selfpresentViewController:imagePickeranimated:YEScompletion:nil];

}

//这个方法是协议UIImagePickerControllerDelegate里面的,选择图片结束的时候就会自动调用

- (void)imagePickerController:(UIImagePickerController*)picker

didFinishPickingImage:(UIImage*)image editingInfo:(nullableNSDictionary *)editingInfo

{

//设置头像

[self.userbtnsetBackgroundImage:imageforState:(UIControlStateNormal)];

//将系统相册消失掉

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

- (void)didReceiveMemoryWarning{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be

recreated.

}

@end

TL4n1EkyƜ�_&�

相关文章

网友评论

      本文标题:ios.访问相册(设置头像)

      本文链接:https://www.haomeiwen.com/subject/tqesjttx.html