剛轉(zhuǎn)行iOS的搬磚工人,在此記錄下這條路上的點(diǎn)點(diǎn)滴滴,共勉
在之前的筆記中夜只,講了如何實現(xiàn)圓形頭像,這里接著上一次的筆記蒜魄,講一下怎么修改頭像(通過圖庫和拍照方式)扔亥。
重點(diǎn)
手勢(UITapGestureRecognizer)、
提示框(UIAlertController)谈为、
相冊拍照(UIImagePickerController)
流程
一般在APP中旅挤,修改頭像是最基本的功能之一了。一般是兩種方式的修改:從相冊選擇圖片或者拍照伞鲫。那么這里就來講一下如何具體實現(xiàn)這個功能粘茄。
Step1:點(diǎn)擊頭像 ->手勢(UITapGestureRecognizer)
首先,點(diǎn)擊頭像秕脓。因為頭像是直接放在ImageView中的柒瓣,默認(rèn)情況下當(dāng)我們點(diǎn)擊頭像的時候,頭像是不會有任何反應(yīng)的吠架。因此芙贫,我們需要給頭像的ImageView添加一個點(diǎn)擊事件,方法如下:
/**
* 添加手勢:也就是當(dāng)用戶點(diǎn)擊頭像了之后傍药,對這個操作進(jìn)行反應(yīng)
*/
//允許用戶交互
_myHeadPortrait.userInteractionEnabled = YES;
//初始化一個手勢
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(alterHeadPortrait:)];
//給ImageView添加手勢
[_myHeadPortrait addGestureRecognizer:singleTap];
}
Step2:彈出選擇提示->提示框(UIAlertController)
通過添加UITapGestureRecognizer(手勢)磺平,系統(tǒng)就知道了我點(diǎn)擊了頭像,接著拐辽,就可以添加具體的方法來進(jìn)行操作了拣挪。在上一步,我為這個手勢的action俱诸,selector(選擇)了一個方法來執(zhí)行菠劝,即alterHeadPortrait:(注意有冒號的),也就是當(dāng)我們點(diǎn)擊了頭像之后乙埃,會執(zhí)行alterHeadPortrait:這個方法:
// 方法:alterHeadPortrait
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
/**
* 彈出提示框
*/
//初始化提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//按鈕:從相冊選擇闸英,類型:UIAlertActionStyleDefault
[alert addAction:[UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:nil]];
//按鈕:拍照,類型:UIAlertActionStyleDefault
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:nil]];
//按鈕:取消介袜,類型:UIAlertActionStyleCancel
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
通過UIAlertController(提示框)這個類甫何,我們創(chuàng)建好了一個提示框,如下:
現(xiàn)在辙喂,當(dāng)我們點(diǎn)擊取消(或者點(diǎn)擊按鈕以外的區(qū)域)提示框就會被自動取消掉,并將提示框收起來。
Step3:從相冊選擇或者拍照選擇頭像->UIImagePickerController
好了巍耗,繞了這么久秋麸,終于開始進(jìn)入主題了,即選擇圖片或者拍照了炬太。那么現(xiàn)在該腫么辦呢灸蟆?好像毫無頭緒的樣子。亲族。炒考。
這里就需要通過UIImagePickerController,通過它霎迫,我們就可以讓我們的APP輕松的實現(xiàn)訪問相冊或者拍照:
操作UIImagePickerController斋枢,需要實現(xiàn)兩個協(xié)議:
<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
進(jìn)行相冊圖片選擇或者相機(jī)拍照的實現(xiàn)代碼如下:
//初始化UIImagePickerController
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//獲取方式1:通過相冊(呈現(xiàn)全部相冊),UIImagePickerControllerSourceTypePhotoLibrary
//獲取方式2知给,通過相機(jī)瓤帚,UIImagePickerControllerSourceTypeCamera
//獲取方方式3,通過相冊(呈現(xiàn)全部圖片)涩赢,UIImagePickerControllerSourceTypeSavedPhotosAlbum
PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//方式1
//允許編輯戈次,即放大裁剪
PickerImage.allowsEditing = YES;
//自代理
PickerImage.delegate = self;
//頁面跳轉(zhuǎn)
[self presentViewController:PickerImage animated:YES completion:nil];
運(yùn)行效果如圖:
Step4:替換頭像->大功告成谒主!
現(xiàn)在朝扼,我們已經(jīng)能夠打開相冊,或者拍照(拍照功能模擬機(jī)無法拍照霎肯,會報錯擎颖,只有用真機(jī)測試)。
可是問題來了观游,現(xiàn)在選擇了新圖片搂捧,確定之后,頭像還是原來的頭像懂缕,并沒有更新允跑。這是因為我們這里還沒有對圖片選擇完全之后的代理方法進(jìn)行實現(xiàn):
//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//定義一個newPhoto,用來存放我們選擇的圖片搪柑。
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
//把newPhono設(shè)置成頭像
_myHeadPortrait.image = newPhoto;
//關(guān)閉當(dāng)前界面聋丝,即回到主界面去
[self dismissViewControllerAnimated:YES completion:nil];
}
大功告成:
完全代碼如下:
#import "ViewController.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *myHeadPortrait;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 調(diào)用setHeadPortrait方法
[self setHeadPortrait];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 方法:設(shè)置頭像樣式
-(void)setHeadPortrait{
// 把頭像設(shè)置成圓形
self.myHeadPortrait.layer.cornerRadius=self.myHeadPortrait.frame.size.width/2;
self.myHeadPortrait.layer.masksToBounds=YES;
// 給頭像加一個圓形邊框
self.myHeadPortrait.layer.borderWidth = 1.5f;
self.myHeadPortrait.layer.borderColor = [UIColor whiteColor].CGColor;
/**
* 添加手勢:也就是當(dāng)用戶點(diǎn)擊頭像了之后,對這個操作進(jìn)行反應(yīng)
*/
//允許用戶交互
_myHeadPortrait.userInteractionEnabled = YES;
//初始化一個手勢
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(alterHeadPortrait:)];
//給imageView添加手勢
[_myHeadPortrait addGestureRecognizer:singleTap];
}
// 方法:alterHeadPortrait
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
/**
* 彈出提示框
*/
//初始化提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//按鈕:從相冊選擇工碾,類型:UIAlertActionStyleDefault
[alert addAction:[UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//初始化UIImagePickerController
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//獲取方式1:通過相冊(呈現(xiàn)全部相冊)弱睦,UIImagePickerControllerSourceTypePhotoLibrary
//獲取方式2,通過相機(jī)渊额,UIImagePickerControllerSourceTypeCamera
//獲取方法3况木,通過相冊(呈現(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){
/**
其實和從相冊選擇一樣求类,只是獲取方式不同,前面是通過相冊屹耐,而現(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<NSString *,id> *)info{
//定義一個newPhoto张症,用來存放我們選擇的圖片仓技。
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
_myHeadPortrait.image = newPhoto;
[self dismissViewControllerAnimated:YES completion:nil];
}
@end