?換頭像 必須添加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(@"打開失敗");
}
}