访问系统相册

//遵守协议

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@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)steuserimage{    

//创建系统相册  

 UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];  

 //设置代理,到@interface后面遵守协议<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

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];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • // ViewController.m // 访问系统相册 // // Created by lanouhn on...
    一起长大的小暧昧阅读 1,792评论 0 0
  • 我们在做应用过程中,难免会遇到要设置用户头像这样的功能,我这里总结了一个调用系统相机,相册的功能实现,写出来与大家...
    张战威ican阅读 5,033评论 0 3
  • 下午上了访问系统相册 圆形的头像半径是宽度的一半 注意设置代理,到@interface后面遵守协议 而且所有的能...
    贝贝贝1010阅读 1,778评论 0 0
  • 自以精魂净, 能当万古悲。 寒空何寂寞, 一任洒清辉。
    青璞阅读 3,247评论 0 0
  • 离婚有什么不利:对自身健康和寿命,对孩子成长不利 离婚的症状:谈话从一个苛刻开始, 双方充满了抱怨和批评,负面情绪...
    MrTimer阅读 1,211评论 0 0

友情链接更多精彩内容