天天看点

iOS ---cell设置头像

<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property(nonatomic,strong) UIImageView *img; //头像

------------------------------------------------------------------------------

            //创建一个imageView

            cell.textLabel.text = @"头像";

            _img =[[UIImageView alloc]init];

            _img.frame=CGRectMake(64, 64, 64, 64);

            //如果服务器返回的数据中 ,图片为空,使用默认图片

            if(_img) {

                //头像

                _img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:iconUrl]]];

            }

            else{

                  _img.image= [UIImage imageNamed:@"default_icon"];

            }

            //把cell右边的变成imageview

            cell.accessoryView =_img;

------------------------------------------------------------------------------

#pragma mark -  选图片

-(void) selectPicture{

    //从底部弹出(iOS 8 的用法)

    UIAlertController *sheetController = [UIAlertController alertControllerWithTitle:@"请选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        //选中拍照之后的操作

        NSLog(@"拍照");

        //资源类型为照相机

        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

        //判断是否有相机

        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){

            UIImagePickerController *picker = [[UIImagePickerController alloc] init];

            picker.delegate =self;

            //设置拍照后的图片可被编辑

            picker.allowsEditing = YES;

            //资源类型为照相机

            picker.sourceType = sourceType;

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

        }else {

            [MBProgressHUD showError:@"该设备无摄像头"];            

            NSLog(@"该设备无摄像头");

        }

    }];

    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

         //选中相册之后的操作

        NSLog(@"相册");

        //从相册选择

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];

        picker.delegate =self;

        //资源类型为图片库

        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        //设置选择后的图片可被编辑

        picker.allowsEditing = YES;

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

    }];

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

    [sheetController addAction:saveAction];

    [sheetController addAction:deleteAction];

    [sheetController addAction:cancelAction];

    [self presentViewController:sheetController animated:YES completion:^{

    }];

}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    // 销毁控制器

    [picker dismissViewControllerAnimated:YES completion:nil];

    // 获得图片

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    // 显示图片

    _img.image=image;

}