美文网首页
上传头像

上传头像

作者: caohuienjoy | 来源:发表于2016-06-29 13:29 被阅读0次

//创建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];

}

相关文章

  • 图片上传与ajax打包form里面的数据

    目录 头像上传的前端处理 头像上传的后端处理 ajax打包form里面的数据 1. 头像上传的前端处理 需求: 效...

  • 移动端上传头像

    上传头像

  • 纯前端实现截图生成头像

    上传头像插件 目的: 帮助开发者快速开发上传头像功能点 背景: 现在b,g能搜到的头像上传插件并不太好用,所以想提...

  • 开发经验总结

    1.头像上传功能 1.拍照或从相册选择, 头像上传。 头像修改成功之后,需要把头像image保存到沙盒。 每次页面...

  • 上传头像

    背景 在小米的面试中,最后一轮被问到了一个场景。即关于在 WebView 下开发一个用户上传头像的场景的完整流程。...

  • 上传头像

    在 User 类中增加字段: 注意:数据库中 user_image 中存储的是图片的路径,而非文件实体,所以数据类...

  • 上传头像

    //创建button和imageView -(void)createUI{ _headImageView = [F...

  • 上传头像

    NSString *base64string=[imagedata base64Encoding];

  • 头像上传

    https://cnodejs.org/topic/51fc8bd944e76d216ab64939

  • 头像上传

    xxxx no.2

网友评论

      本文标题:上传头像

      本文链接:https://www.haomeiwen.com/subject/hcdtjttx.html