澳门威利斯人_威利斯人娱乐「手机版」

来自 澳门威利斯人 2020-03-26 01:52 的文章
当前位置: 澳门威利斯人 > 澳门威利斯人 > 正文

实现修改头像,点击修改头像

// 方法:alterHeadPortrait-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];}

Step3:从相册接收依然拍照采用头像->UIImagePickerController

     [self presentViewController:imagePicker animated:YES completion:nil];        

图片 1选用图片,自由裁剪

差不离流程

- (void)readImageFromAlbum {  

手势(UITapGestureRecognizer)、提示框(UIAlertController)、相册拍照(UIImagePickerController)

日常在APP中,校订头疑似最基本的效应之一了。日常是二种方法的改良:从相册选取图片或许拍照。那么这里就来说一下怎么具体贯彻这么些职能。

    } else {     

//PickerImage完成后的代理方法- imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ //定义一个newPhoto,用来存放我们选择的图片。 UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"]; //把newPhono设置成头像 _myHeadPortrait.image = newPhoto; //关闭当前界面,即回到主界面去 [self dismissViewControllerAnimated:YES completion:nil];}

图片 2

      // 决断选择的情势是还是不是为相机形式,若无则弹窗警示  

<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

开展相册图片采取还是相机拍片的得以达成代码如下:

  [self presentViewController:imagePicker animated:YES completion:nil];  // 显示相册

在前边的笔记中,讲了何等促成圆形头像,这里接着上一回的笔记,讲一下怎么改进头像(通过图库和壁画方式)。

/**

   // 钦定代理,代理需服从,协议   

Step4:替换头像->马到成功!

以后,当大家点击撤消(可能点击按键以外的区域)提醒框就能够被电动撤废掉,并将唤起框收起来。

   }

图片 3QQ的改变头像操作

图片 4

           imagePicker.delegate = self;           

现在,大家曾经能够展开相册,大概拍照(拍照效用模拟机不能拍戏,会报错,唯有用真机测量检验)。但是难题来了,未来采取了新图片,鲜明之后,头像照旧原本的头像,并从未改善。那是因为大家这里还尚无对图片选取完全之后的代理方法开展落到实处:

#import"ViewController.h"

@interfaceViewController()

@property(strong,nonatomic)IBOutletUIImageView*myHeadPortrait;

@end

@implementationViewController

- (void)viewDidLoad

{  

[superviewDidLoad];

//  调用setHeadPortrait方法

[selfsetHeadPortrait];

}

#pragma mark - 设置头像-(void卡塔尔(قطر‎setHeadPortrait {    //    //  把头像设置成圆形//    self.iconImg.layer.cornerRadius=self.iconImg.frame.size.width/2;//    self.iconImg.layer.masksToBounds=YES;    //  给头像加一个圆形边框    self.iconImg.layer.borderWidth = 1.5f;    self.iconImg.layer.borderColor = [UIColor whiteColor].CGColor;    /**    *  加多手势:也即是当顾客点击头像了之后,对这一个操作举办反应    */    //允许客户人机联作    _iconImg.userInteractionEnabled = YES;    //初阶化一个手势    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self                                                            action:@selector(alterHeadPortrait:)];    //给imageView增加手势    [_iconImg 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:通过相册(显示总体相册),UIImagePickerControllerSourceTypePhotoLibrary          //获取格局2,通过相机,UIImagePickerControllerSourceTypeCamera          //获取方式3,通过相册(展现总体图形),UIImagePickerControllerSourceTypeSavedPhotosAlbum          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];}//PickerImage实现后的代理方法- (voidState of QatarimagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{

//定义三个newPhoto,用来寄放在我们选择的图样。

UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];

self.iconImg.image = newPhoto;

[self dismissViewControllerAnimated:YES completion:nil];

}

                                           }];  

图片 5校订头像成功

*  增多手势:也正是当客户点击头像精通后,对那几个操作实行反馈

  UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消"         style:UIAlertActionStyleCancel                                                  handler:nil];   

丰功伟大事业告成:

图片 6

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

Step2:弹出选用提醒->指示框(UIAlertController)

//初始化UIImagePickerControllerUIImagePickerController*PickerImage = [[UIImagePickerControlleralloc]init];//获取格局1:通过相册(展现总体相册),UIImagePickerControllerSourceTypePhotoLibrary//获取方式2,通过相机,UIImagePickerControllerSourceTypeCamera//获取方格局3,通过相册(展现总体图纸),UIImagePickerControllerSourceTypeSavedPhotosAlbumPickerImage.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//方式1//允许编写制定,即放大裁剪PickerImage.allowsEditing=YES;//自代理PickerImage.delegate=self;//页面跳转[selfpresentViewController:PickerImage animated:YEScompletion:nil];

  [self presentViewController: alertController animated:YES completion:nil];}

Step1:点击头像 ->手势(UITapGestureRecognizer)

空格自己分手下。。。。xcode复制过来就这么了

}

先是,点击头像。因为头疑似直接放在ImageView中的,暗许境况下当我们点击头像的时候,头疑似不会有别的反响的。由此,咱们要求给头像的ImageView增添二个点击事件,方法如下:

好了,绕了这么久,终于起始步入正题了,即选择图片或然拍照了。那么未来该咋做吧?好像毫无头绪的模范。。。

- (IBAction)tapGes:(UITapGestureRecognizer *)tapges {  

图片 7点击头像,弹出提醒框未来,当我们点击废除(可能点击按钮以外的区域)提示框就能够被自动撤消掉,并将唤起框收起来。

Step1:点击头像 ->手势(UITapGestureRecognizer)

        [self presentViewController:alert animated:YES completion:nil];      

 /** * 添加手势:也就是当用户点击头像了之后,对这个操作进行反应 */ //允许用户交互 _myHeadPortrait.userInteractionEnabled = YES; //初始化一个手势 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alterHeadPortrait:)]; //给ImageView添加手势 [_myHeadPortrait addGestureRecognizer:singleTap]; }

QQ的调换头像操作

  tapGes.delegate = self;   

通过UIAlertController这几个类,大家创建好了叁个提示框,如下:

运维效果如图:

      imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;     

#import "ViewController.h"@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>@property (strong, nonatomic) IBOutlet UIImageView *myHeadPortrait;@end@implementation ViewController- viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 调用setHeadPortrait方法 [self setHeadPortrait];}- didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}// 方法:设置头像样式-setHeadPortrait{ // 把头像设置成圆形 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; /** * 添加手势:也就是当用户点击头像了之后,对这个操作进行反应 */ //允许用户交互 _myHeadPortrait.userInteractionEnabled = YES; //初始化一个手势 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alterHeadPortrait:)]; //给imageView添加手势 [_myHeadPortrait addGestureRecognizer:singleTap]; }// 方法:alterHeadPortrait-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:通过相册,UIImagePickerControllerSourceTypePhotoLibrary //获取方式2,通过相机,UIImagePickerControllerSourceTypeCamera //获取方法3,通过相册,UIImagePickerControllerSourceTypeSavedPhotosAlbum 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];}//PickerImage完成后的代理方法- imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ //定义一个newPhoto,用来存放我们选择的图片。 UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"]; _myHeadPortrait.image = newPhoto; [self dismissViewControllerAnimated:YES completion:nil];}@end

操作UIImagePickerController,须求达成多个左券:

UIAlertAction *camera = [UIAlertAction actionWithTitle:@"相机"       style:UIAlertActionStyleDefault                                                  handler:^(UIAlertAction * _Nonnull action) {   

刚转行iOS的搬砖工人,在这里记录下那条路上的一丝一毫,共勉

//  方法:alterHeadPortrait-(void)alterHeadPortrait:(UITapGestureRecognizer*)gesture{/**

 [alertController addAction:camera]; 

运维效果如图:

通过UIAlertController(提示框)以此类,大家创立好了一个提醒框,如下:

                           }];  

本文由澳门威利斯人发布于澳门威利斯人,转载请注明出处:实现修改头像,点击修改头像

关键词: 澳门威利斯人 IOS 笔记 头像 iOS技术点