换头像 必须添加info.plist 的两个权限 摄像头和相册
添加协议 添加成员变量
<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UITableView *tableV;
UIImagePickerController *pickerC;
UIImageView *imageV;
UIImage *myImage;
}
@property (nonatomic,strong)NSMutableArray *Marr;
@end
------------------------ 初始化图片选择器 ----------------------
pickerC = [[UIImagePickerController alloc]init];
pickerC.delegate = self;
pickerC.allowsEditing = YES;
----------------------- 实现代理方法 -----------------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.Marr.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellID = @"cyy"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID]; } cell.textLabel.text = self.Marr[indexPath.row]; NSInteger Tag = 10; imageV = [cell.contentView viewWithTag:Tag]; if (!imageV) { imageV = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 80, 10, 60, 60)]; imageV.backgroundColor = [UIColor redColor]; imageV.image = [UIImage imageNamed:@"1"]; [cell.contentView addSubview:imageV]; } imageV.image = myImage; return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row == 0){ UIAlertController *alert = [[UIAlertController alloc]init]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"打开相机"); [self openXJ]; }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"打开相册"); [self openXC]; }]; [alert addAction:action]; [alert addAction:action1]; [alert addAction:action2]; [self presentViewController:alert animated:YES completion:nil]; } }- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary*)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
myImage = image;
[tableV reloadData];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)openXJ{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
pickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:pickerC animated:YES completion:nil];
}else{
NSLog(@"打开失败");
}
}
-(void)openXC{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerC animated:YES completion:nil];
}else{
NSLog(@"打开失败");
}
}