訪問系統(tǒng)相冊(cè)

//遵守協(xié)議

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@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; ? ?

//超過內(nèi)切圓的部分是否切割掉 ?

?self.userbtn.layer.masksToBounds=YES; ?

//添加點(diǎn)擊事件:去訪問系統(tǒng)相冊(cè) ?

[self.userbtn addTarget:self action:@selector(setuserimage) forControlEvents:(UIControlEventTouchUpInside)]; ?

//將按鈕添加到屏幕上 ?

[self.view addSubview:self.userbtn];}

-(void)steuserimage{ ? ?

//創(chuàng)建系統(tǒng)相冊(cè) ?

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

?//設(shè)置代理,到@interface后面遵守協(xié)議<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

imagePicker.delegate=self; ? ?

//彈出系統(tǒng)相冊(cè) ? ?

[self presentViewController:imagePicker animated:YES completion:nil]; ? ?}

//這個(gè)方法是協(xié)議UIImagePickerControllerDelegate里面的,選擇圖片結(jié)束就會(huì)自動(dòng)調(diào)用

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary*)editingInfo{

//設(shè)置頭像

[self.userbtn setBackgroundImage:image forState:(UIControlStateNormal)];

//將系統(tǒng)相冊(cè)消失掉

[picker dismissViewControllerAnimated:YES completion:nil];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容