美文网首页首页投稿(暂停使用,暂停投稿)
iOS开发 手机选取相册 相机照片 做头像 封面

iOS开发 手机选取相册 相机照片 做头像 封面

作者: 流浪猫121 | 来源:发表于2017-07-13 15:07 被阅读0次

    // 方法:设置头像样式
    -(void)setHeadPortrait{

    // self.liveHidImage.layer.masksToBounds=YES;

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

    }

    UIImagePickerControllerDelegate

    //PickerImage完成后的代理方法

    • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
      //定义一个newPhoto,用来存放我们选择的图片。
      UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
      _liveHidImage.image = newPhoto;
      [self dismissViewControllerAnimated:YES completion:nil];
      }

    相关文章

      网友评论

        本文标题:iOS开发 手机选取相册 相机照片 做头像 封面

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