#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView *UserHeadImage;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *headBackImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, widget_height(270))];
[headBackImage setImage:[UIImage imageNamed:@"Mine_Background"]];
headBackImage.userInteractionEnabled = YES;
[self.view addSubview:headBackImage];
//用户默认头像
UIButton *headBtn = [[UIButton alloc]initWithFrame:CGRectMake((Main_Width - widget_height(169))/2, widget_height(35), widget_height(169), widget_height(169))];
[headBackImage addSubview:headBtn];
UserHeadImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, widget_height(169), widget_height(169))];
[UserHeadImage setImage:[UIImage imageNamed:@"Mine_UserHead"]];
UserHeadImage.layer.masksToBounds = YES;
UserHeadImage.layer.cornerRadius = widget_height(169)/2;
[headBackImage addSubview:headBtn];
[headBtn addSubview:UserHeadImage];
[headBtn addTarget:self action:@selector(tapToUserInfoPage) forControlEvents:UIControlEventTouchUpInside];
}
- (void)tapToUserInfoPage {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE_8_0
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *photoLibraryAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self getImageFromPhotoLibrary];
}];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self getImageFromCamera];
}];
[alertController addAction:cancelAction];
[alertController addAction:photoLibraryAction];
[alertController addAction:cameraAction];
[self presentViewController:alertController animated:YES completion:NULL];
#endif
#if __IPHONE_OS_VERSION_MAX_ALLOWED < _IPHONE_8_0
UIActionSheet *alertView = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"相机", nil];
[alertView showInView:self.view];
#endif
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
[self getImageFromPhotoLibrary];
break;
case 1:
[self getImageFromCamera];
break;
default:
break;
}
}
//从相册获取
- (void)getImageFromPhotoLibrary
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[picker.navigationBar setBackgroundImage:[UIImage imageNamed:@"partner_navi"] forBarMetrics:UIBarMetricsDefault];
[picker.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:NULL];
}
//调用相机
- (void)getImageFromCamera
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:NULL];
}
else
{
// UIAlertController*alert = [UIAlertController alertControllerWithTitle:@"tishi" message:@"相机不能用" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){}];[alert addAction:confirm];
// [self presentViewController:alert animated:YES completion:nil];
[self creatAlert:@"相机不能用"];
}
}
#pragma mark - image picker delegte 调用系统相机
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取图片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
UserHeadImage.image = image;
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)creatAlert:(NSString *)msg
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED < _IPHONE_8_0
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
#endif
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE_8_0
UIAlertController *avc = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[avc addAction:action];
[self presentViewController:avc animated:YES completion:^{
}];
#endif
}
@end
#ifndef Prefix_pch
#define Prefix_pch
//获取屏幕高宽
#define Main_Height [[UIScreen mainScreen] bounds].size.height
#define Main_Width [[UIScreen mainScreen] bounds].size.width
#define widget_width(w) ((w) * Main_Width / 750)
#define widget_height(h) ((h) * Main_Height / 1334)
#endif /* Prefix_pch */
来源:https://www.cnblogs.com/CY-miaomiako/p/5440059.html