1.性別選擇器的制作
#pragma mark----- 選擇性別彈出框
-(void)alterSexPortrait{
/* 彈出提示框
*/
//初始化提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//按鈕:男
[alert addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.userSexText.text=@"男";
self.userSexText.textColor = [UIColor blackColor];
}]];
//按鈕:女
[alert addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
self.userSexText.text=@"女";
self.userSexText.textColor = [UIColor blackColor];
}]];
//按鈕:取消,類型:UIAlertActionStyleCancel
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
2.日期選擇器
調(diào)用XLsn0wPickerTimer.h XLsn0wPickerTimer.m
協(xié)議方法
#pragma mark--選擇出生年月
- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
self.userDateText.text = [NSString stringWithFormat:@"%ld年-%ld月-%ld日", year, month, day];
self.userDateText.textColor = [UIColor blackColor];
}
在調(diào)用日期選擇器的方法中
NSLog(@"日期選擇");
XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
pickerDate.xlsn0wDelegate = self;
[pickerDate show];
3.相冊相機選擇器
若得到的相片需要裁圓,則
_image.layer.masksToBounds = YES;
_image.layer.cornerRadius = 40.0;
首先在Info.plist中開啟相機以及相冊權(quán)限
DEACDDBD-ED97-44B9-B43A-E5DA69FC5F01.png
解決調(diào)用相機以及相冊時為英文的相冊界面(設(shè)置為YES)
47ECF0C3-D9D4-4DB0-AB3C-06F0BEF28076.png
需要開啟的界面.m中
@interface UserInfoVC ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
#pragma mark----- 選擇照片彈出框
-(void)alterHeadPortrait{
/* 彈出提示框
*/
//初始化提示框
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];
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)在,我們要通過相機的方式
*/
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//獲取方式:通過相機
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];
}
#pragma mark-----PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//定義一個newPhoto,用來存放我們選擇的圖片。
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
self.userImage.image = newPhoto;
self.userImage.layer.cornerRadius = 40;
[self dismissViewControllerAnimated:YES completion:nil];
}