上传头像

//创建button和imageView

-(void)createUI{

_headImageView = [FactoryUI createImageViewWithFrame:CGRectMake(0, -originalImageHeight, SCREEN_W, originalImageHeight) imageName:@"my-backgroud.jpg"];

_iconView = [FactoryUI createImageViewWithFrame:CGRectMake(SCREEN_W/2-28, 80, 60, 60) imageName:@"my-icon"];

_iconView.backgroundColor = [UIColor clearColor];

_iconView.layer.cornerRadius = 30;

_iconView.layer.masksToBounds = YES;

_loginButton = [FactoryUI createButtonWithFrame:CGRectMake(SCREEN_W/2-40, 150, 80, 20) title:@"上传头像" titleColor:[UIColor whiteColor] backgroundColor:[UIColor redColor] type:UIButtonTypeSystem target:self selector:@selector(loginButtonClick)];

[_headImageView addSubview:_iconView];

[_headImageView addSubview:_loginButton];

[self savePhoto];

}

#pragma mark - 保存图片

- (void)savePhoto{       

NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];    

UIImage *savedImage = [UIImage imageWithContentsOfFile:fullPath];  

if (!savedImage) {       

_iconView.image = [UIImage imageNamed:@"my-icon"];  

}    else{   

    _iconView.image = savedImage;  

}  

_iconView.layer.cornerRadius = 30;  

}

#pragma mark - 登录button事件响应

- (void)loginButtonClick

{  

UIAlertController *alertroller=[UIAlertController alertControllerWithTitle:@"提示" message:@"请上传图片" preferredStyle:UIAlertControllerStyleAlert];   

[alertroller addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {    }]];

  [alertroller addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        [self toTakePhoto];    }]];  

[alertroller addAction:[UIAlertAction actionWithTitle:@"上传图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        [self toPhotoLib];    }]];    [self presentViewController:alertroller animated:YES completion:nil];}

#pragma mark - 上传图片

- (void)toPhotoLib{       

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;   

imagePickerController.delegate = self;   

imagePickerController.allowsEditing = YES;   

[imagePickerController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar"] forBarMetrics:UIBarMetricsCompact];        imagePickerController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};  

[self presentViewController:imagePickerController animated:YES completion:nil];

}

//手机拍照

-(void)toTakePhoto{   

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];   

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

      imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;        imagePickerController.delegate = self;      

imagePickerController.allowsEditing = YES;      

[imagePickerController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bar"] forBarMetrics:UIBarMetricsCompact];                imagePickerController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};      

[self presentViewController:imagePickerController animated:YES completion:nil];  

} else {    

  UIAlertController *alertroller=[UIAlertController alertControllerWithTitle:@"提示" message:@"无可用摄像头" preferredStyle:UIAlertControllerStyleAlert];       

[alertroller addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        }]];        [self presentViewController:alertroller animated:YES completion:nil];    }

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{

[picker dismissViewControllerAnimated:YES completion:nil];

_iconView.image =info[UIImagePickerControllerEditedImage];

[self saveImage:_iconView.image withName:@"currentImage.png"];

}

#pragma mark - 保存图片至本地沙盒

- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName

{

NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.8);

// 获取沙盒目录

NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];

// 将图片写入文件

[imageData writeToFile:fullPath atomically:NO];

}

你可能感兴趣的:(上传头像)