天天看點

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;

}