美文网首页
iOS----简单的性别,日期,相机相册选择器制作

iOS----简单的性别,日期,相机相册选择器制作

作者: 旷世流年 | 来源:发表于2017-05-11 19:44 被阅读0次

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.日期选择器

调用XLsn0wPickerTimer.h  XLsn0wPickerTimer.m
协议方法
#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];
}
在调用日期选择器的方法中
  NSLog(@"日期选择");
    XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
    pickerDate.xlsn0wDelegate = self;
    
    [pickerDate show];

3.相册相机选择器

若得到的相片需要裁圆,则
 _image.layer.masksToBounds = YES;
 _image.layer.cornerRadius = 40.0;

首先在Info.plist中开启相机以及相册权限

DEACDDBD-ED97-44B9-B43A-E5DA69FC5F01.png

解决调用相机以及相册时为英文的相册界面(设置为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;
        //页面跳转
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    //按钮:拍照,类型:UIAlertActionStyleDefault
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
        /**
         其实和从相册选择一样,只是获取方式不同,前面是通过相册,而现在,我们要通过相机的方式
         */
        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];
}

相关文章

网友评论

      本文标题:iOS----简单的性别,日期,相机相册选择器制作

      本文链接:https://www.haomeiwen.com/subject/qwcatxtx.html