相册
<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
-(void)setHeadPortrait
{
self.ivHeadPortrait.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(alterHeadPortrait:)];
[self.ivHeadPortrait addGestureRecognizer:singleTap];
}
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
PickerImage.allowsEditing = YES;
PickerImage.delegate = self;
[self presentViewController:PickerImage animated:YES completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
PickerImage.allowsEditing = YES;
PickerImage.delegate = self;
[self presentViewController:PickerImage animated:YES completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
self.ivHeadPortrait.image = newPhoto;
[self dismissViewControllerAnimated:YES completion:nil];
}