訪問系統(tǒng)相冊

下午上了訪問系統(tǒng)相冊 ?

圓形的頭像半徑是寬度的一半

注意設置代理,到@interface后面遵守協(xié)議

而且所有的能看得到的UI控件創(chuàng)建初始化呢方式都可以采用alloc initWithFrame




//

//ViewController.m

//訪問系統(tǒng)相冊

//

//Created bylanouhn on 16/7/12.

//Copyright?2016年lanouhn.

All rights reserved.

//

#import"ViewController.h"

//遵守協(xié)議

@interfaceViewController()

@property(nonatomic,strong)UIButton*userbtn;

@end

@implementationViewController

- (void)viewDidLoad{

[superviewDidLoad];

//所有的能看得到的UI控件創(chuàng)建初始化呢方式都可以采用alloc initWithFrame

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

//設置顏色

self.userbtn.backgroundColor= [UIColorredColor];

//設置圓形半徑

self.userbtn.layer.cornerRadius=40;

self.userbtn.layer.masksToBounds=YES;

//添加點擊事件:去訪問系統(tǒng)相冊

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

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

[self.viewaddSubview:self.userbtn];

}

//訪問系統(tǒng)相冊

-(void)setUserImage

{

UIImagePickerController*imagePicker = [[UIImagePickerControlleralloc]init];

//設置代理,到@interface后面遵守協(xié)議

UIImagePickerControllerDelegate>

imagePicker.delegate=self;

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

[selfpresentViewController:imagePickeranimated:YEScompletion:nil];

}

//這個方法是協(xié)議UIImagePickerControllerDelegate里面的,選擇圖片結(jié)束的時候就會自動調(diào)用

- (void)imagePickerController:(UIImagePickerController*)picker

didFinishPickingImage:(UIImage*)image

editingInfo:(nullableNSDictionary *)editingInfo

{

//設置頭像

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

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

[picker

dismissViewControllerAnimated:YEScompletion:nil];

}

- (void)didReceiveMemoryWarning{

[superdidReceiveMemoryWarning];

// Dispose

of any resources that can be recreated.

}

@end

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

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

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