#import"ViewController.h"
@interfaceViewController()
@property(strong,nonatomic)IBOutletUIImageView*myHeadPortrait;
@end
@implementationViewController
- (void)viewDidLoad
{??
[superviewDidLoad];
//? 調(diào)用setHeadPortrait方法
[selfsetHeadPortrait];
}
#pragma mark - 設(shè)置頭像-(void)setHeadPortrait {? ? //? ? //? 把頭像設(shè)置成圓形//? ? self.iconImg.layer.cornerRadius=self.iconImg.frame.size.width/2;//? ? self.iconImg.layer.masksToBounds=YES;? ? //? 給頭像加一個(gè)圓形邊框? ? self.iconImg.layer.borderWidth = 1.5f;? ? self.iconImg.layer.borderColor = [UIColor whiteColor].CGColor;? ? /**? ? *? 添加手勢(shì):也就是當(dāng)用戶點(diǎn)擊頭像了之后梨与,對(duì)這個(gè)操作進(jìn)行反應(yīng)? ? */? ? //允許用戶交互? ? _iconImg.userInteractionEnabled = YES;? ? //初始化一個(gè)手勢(shì)? ? UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? action:@selector(alterHeadPortrait:)];? ? //給imageView添加手勢(shì)? ? [_iconImg addGestureRecognizer:singleTap];? ? }//? 方法:alterHeadPortrait-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{? ? /**? ? ? *? 彈出提示框? ? ? */? ? //初始化提示框? ? UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];? ? //按鈕:從相冊(cè)選擇如贷,類型:UIAlertActionStyleDefault? ? [alert addAction:[UIAlertAction actionWithTitle:@"從相冊(cè)選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? //初始化UIImagePickerController? ? ? ? ? UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];? ? ? ? ? //獲取方式1:通過相冊(cè)(呈現(xiàn)全部相冊(cè))尚猿,UIImagePickerControllerSourceTypePhotoLibrary? ? ? ? ? //獲取方式2楣富,通過相機(jī)纹蝴,UIImagePickerControllerSourceTypeCamera? ? ? ? ? //獲取方法3塘安,通過相冊(cè)(呈現(xiàn)全部圖片)忍捡,UIImagePickerControllerSourceTypeSavedPhotosAlbum? ? ? ? ? PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;? ? ? ? ? //允許編輯砸脊,即放大裁剪? ? ? ? ? PickerImage.allowsEditing = YES;? ? ? ? ? //自代理? ? ? ? ? PickerImage.delegate = self;? ? ? ? ? //頁面跳轉(zhuǎn)? ? ? ? ? [self presentViewController:PickerImage animated:YES completion:nil];? ? }]];? ? //按鈕:拍照,類型:UIAlertActionStyleDefault? ? [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){? ? ? ? ? /**? ? ? ? ? 其實(shí)和從相冊(cè)選擇一樣,只是獲取方式不同埃疫,前面是通過相冊(cè)熔恢,而現(xiàn)在臭笆,我們要通過相機(jī)的方式? ? ? ? ? */? ? ? ? ? UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];? ? ? ? ? //獲取方式:通過相機(jī)? ? ? ? ? PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;? ? ? ? ? PickerImage.allowsEditing = YES;? ? ? ? ? PickerImage.delegate = self;? ? ? ? ? [self presentViewController:PickerImage animated:YES completion:nil];? ? }]];? ? //按鈕:取消,類型:UIAlertActionStyleCancel? ? [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];? ? [self presentViewController:alert animated:YES completion:nil];}//PickerImage完成后的代理方法- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
//定義一個(gè)newPhoto茵乱,用來存放我們選擇的圖片。
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
self.iconImg.image = newPhoto;
[self dismissViewControllerAnimated:YES completion:nil];
}
空格自己分開下渠羞。荧恍。屯吊。盒卸。xcode復(fù)制過來就這樣了
大致流程
一般在APP中,修改頭像是最基本的功能之一了糟需。一般是兩種方式的修改:從相冊(cè)選擇圖片或者拍照洲押。那么這里就來講一下如何具體實(shí)現(xiàn)這個(gè)功能杈帐。
Step1:點(diǎn)擊頭像 ->手勢(shì)(UITapGestureRecognizer)
QQ的更換頭像操作
首先挑童,點(diǎn)擊頭像。因?yàn)轭^像是直接放在ImageView中的尽楔,默認(rèn)情況下當(dāng)我們點(diǎn)擊頭像的時(shí)候阔馋,頭像是不會(huì)有任何反應(yīng)的娇掏。因此婴梧,我們需要給頭像的ImageView添加一個(gè)點(diǎn)擊事件塞蹭,方法如下:
/**
*? 添加手勢(shì):也就是當(dāng)用戶點(diǎn)擊頭像了之后竟坛,對(duì)這個(gè)操作進(jìn)行反應(yīng)
*///允許用戶交互_myHeadPortrait.userInteractionEnabled=YES;//初始化一個(gè)手勢(shì)UITapGestureRecognizer*singleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(alterHeadPortrait:)];//給ImageView添加手勢(shì)[_myHeadPortrait addGestureRecognizer:singleTap];? ? }
Step2:彈出選擇提示->提示框(UIAlertController)
通過添加UITapGestureRecognizer(手勢(shì)),系統(tǒng)就知道了我點(diǎn)擊了頭像洼冻,接著,就可以添加具體的方法來進(jìn)行操作了叔营。在上一步所宰,我為這個(gè)手勢(shì)的action婴谱,selector(選擇)了一個(gè)方法來執(zhí)行躯泰,即alterHeadPortrait:(注意有冒號(hào)的)瘟裸,也就是當(dāng)我們點(diǎn)擊了頭像之后诵竭,會(huì)執(zhí)行alterHeadPortrait:這個(gè)方法:
//? 方法:alterHeadPortrait-(void)alterHeadPortrait:(UITapGestureRecognizer*)gesture{/**
*? 彈出提示框
*///初始化提示框UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];//按鈕:從相冊(cè)選擇超棺,類型:UIAlertActionStyleDefault[alert addAction:[UIAlertActionactionWithTitle:@"從相冊(cè)選擇"style:UIAlertActionStyleDefaulthandler:nil]];//按鈕:拍照件相,類型:UIAlertActionStyleDefault[alert addAction:[UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDefaulthandler:nil]];//按鈕:取消夜矗,類型:UIAlertActionStyleCancel[alert addAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil]];? ? [selfpresentViewController:alert animated:YEScompletion:nil];}
通過UIAlertController(提示框)這個(gè)類罢荡,我們創(chuàng)建好了一個(gè)提示框对扶,如下:
點(diǎn)擊頭像笼才,彈出提示框
現(xiàn)在昂羡,當(dāng)我們點(diǎn)擊取消(或者點(diǎn)擊按鈕以外的區(qū)域)提示框就會(huì)被自動(dòng)取消掉摔踱,并將提示框收起來赴穗。
Step3:從相冊(cè)選擇或者拍照選擇頭像->UIImagePickerController
好了膀息,繞了這么久潜支,終于開始進(jìn)入主題了,即選擇圖片或者拍照了。那么現(xiàn)在該腫么辦呢裁替?好像毫無頭緒的樣子弱判。开伏。遭商。
這里就需要通過UIImagePickerController巫玻,通過它仍秤,我們就可以讓我們的APP輕松的實(shí)現(xiàn)訪問相冊(cè)或者拍照:
操作UIImagePickerController徒扶,需要實(shí)現(xiàn)兩個(gè)協(xié)議:
進(jìn)行相冊(cè)圖片選擇或者相機(jī)拍照的實(shí)現(xiàn)代碼如下:
//初始化UIImagePickerControllerUIImagePickerController*PickerImage = [[UIImagePickerControlleralloc]init];//獲取方式1:通過相冊(cè)(呈現(xiàn)全部相冊(cè))导坟,UIImagePickerControllerSourceTypePhotoLibrary//獲取方式2圈澈,通過相機(jī)康栈,UIImagePickerControllerSourceTypeCamera//獲取方方式3啥么,通過相冊(cè)(呈現(xiàn)全部圖片)菠秒,UIImagePickerControllerSourceTypeSavedPhotosAlbumPickerImage.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//方式1//允許編輯氯迂,即放大裁剪PickerImage.allowsEditing=YES;//自代理PickerImage.delegate=self;//頁面跳轉(zhuǎn)[selfpresentViewController:PickerImage animated:YEScompletion:nil];
運(yùn)行效果如圖: