设置两个代理即:UINavigationControllerDelegate,UIImagePickerControllerDelegate
-(void)headBClick{
UIImagePickerController *controlImg=[[UIImagePickerController alloc] init];
//设置代理
controlImg.delegate=self;
//是否编辑
controlImg.allowsEditing=YES;
//初始化弹出对象
UIAlertController *alterC=[UIAlertController alertControllerWithTitle:@"" message:@"选择照片" preferredStyle:UIAlertControllerStyleActionSheet];
//初始化选项事件
UIAlertAction *action=[UIAlertAction actionWithTitle:@"选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//设置类型
controlImg.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
//弹出图片视图
[self presentViewController:controlImg animated:YES completion:^{
}];
}];
[alterC addAction:action];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertAction *action2=[UIAlertAction actionWithTitle:@"选择相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//设置类型
controlImg.sourceType=UIImagePickerControllerSourceTypeCamera;
//弹出
[self presentViewController:controlImg animated:YES completion:^{
}];
}];
[alterC addAction:action2];
}
[self presentViewController:alterC animated:YES completion:^{
}];
//初始化选项事件
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alterC addAction:action3];
}
#pragma Mark 取消照片代理
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
// NSLog(@"%@",info);
[headB setImage:info[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
[self dismissViewControllerAnimated:YES completion:^{
}];
}