前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从相册中选择或拍照设置并上传头像图片设置头像

从相册中选择或拍照设置并上传头像图片设置头像

作者头像
VV木公子
发布2018-06-05 16:03:46
6.5K0
发布2018-06-05 16:03:46
举报
文章被收录于专栏:TechBoxTechBox

相信很多app中都有通过拍照或者从相册中选择的方式设置并上传头像的功能。如下是我之前一个项目中通过相册或者拍照获取图片的一个功能(照片来源于网络)。现在把代码贴出来,大家使用时(点击imageView或者button时),只需要调用- (void)didTapHeaderImageView方法,即可实现通过相册或者拍照的方式获取照片的功能。

照片来源于网络
照片来源于网络

照片来源于网络

设置头像

代码语言:javascript
复制
- (void)didTapHeaderImageView
{
    NSLog(@"点击了头像");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"照片" message:@"拍照或者从相册中选择照片" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) { // 点击了拍照按钮"
        
        // 拍照
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            
            picker.allowsEditing = YES;
            picker.sourceType = sourceType;
            [self presentViewController:picker animated:YES completion:nil];
        }else
        {
            NSLog(@"模拟其中无法打开照相机,请在真机中使用");
        }
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) { // 点击了相册按钮
        
        
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { // 没有相册
            return;
        }
        
        
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.delegate = self;
        
        pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:pickerVC animated:YES completion:nil];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {
        
    }];
    
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    
    [self presentViewController:alertController animated:YES completion:nil];

}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    if ([type isEqualToString:@"public.image"])
    {
        
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
        
        
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        NSString *imageName = [NSString stringWithFormat:@"/headerImage.png"];
    
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:imageName] contents:data attributes:nil];
        //        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
        
        //得到沙盒中图片的完整路径
        NSString *filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  imageName];
        image = [UIImage imageWithContentsOfFile:filePath];
        
        //        NSData * imageData = UIImageJPEGRepresentation(image,1);
        //        NSInteger length = [imageData length]/1000;
        //        NSLog(@"%ld",length);
        
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [self.tableView addSubview:btn];
        btn.backgroundColor = [UIColor redColor];
        [self.headerImageView setImage:image];
    }
}

// 用户取消了操作
- (void)imagePickerControllerDidCancel:(nonnull UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.04.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 设置头像
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档