相信很多app中都有通过拍照或者从相册中选择的方式设置并上传头像的功能。如下是我之前一个项目中通过相册或者拍照获取图片的一个功能(照片来源于网络)。现在把代码贴出来,大家使用时(点击imageView或者button时),只需要调用- (void)didTapHeaderImageView方法,即可实现通过相册或者拍照的方式获取照片的功能。
设置头像
- (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];
}
文/VV木公子(简书作者)
PS:如非特别说明,所有文章均为原创作品,著作权归作者所有,转载转载请联系作者获得授权,并注明出处,所有打赏均归本人所有!
如果您是iOS开发者,请关注本人,或者对本篇文章感兴趣,请点击喜欢,后续会更新更多相关文章!敬请期待!