【iOS】一个完整的简单的调用系统相机和相册设置头像
来源:互联网 发布:f16战斗机最新型号数据 编辑:程序博客网 时间:2024/05/22 04:38
1.Xcode8,iOS10的权限设置(不设置会崩溃):
找到项目的info.plist文件,右键open As,以Source Code的形式打开,将以下代码添加进去:
相机权限设置:
<key>NSCameraUsageDescription</key>
<string>cameraDesciption</string>
相册权限设置:
<key>NSPhotoLibraryUsageDescription</key>
<string>photoLibraryDesciption</string>
设置好之后,clean一下。
2.在代码中设置代理
@interface MineCtrl ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>@property(nonatomic,strong)UIImagePickerController *imagePicker;@property(nonatomic,strong)UIImageView *headShot;
3.设置相关属性
_imagePicker = [[UIImagePickerController alloc]init]; _imagePicker.delegate = self;
_headShot = [[UIImageView alloc]init]; NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/currentImage.png"];
_headShot.frame = CGRectMake((SCREEN_WIDTH-80)/2, (SCREEN_HEIGHT/4-80)/2, 80, 80);
<pre name="code" class="objc"> //不设置contentMode,图片会被压扁 _headShot.contentMode = UIViewContentModeScaleAspectFill; [_headShot setClipsToBounds:YES]; //将选择的图片显示出来 _headShot.image = [UIImage imageWithContentsOfFile:filePath];
_headShot.backgroundColor = [UIColor grayColor];
_headShot.layer.masksToBounds = YES; _headShot.layer.cornerRadius = 40; _headShot.layer.borderColor = [[UIColor whiteColor]CGColor];
//添加点击手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ClickHandle)]; [tapGesture setNumberOfTapsRequired:1]; [_headShot addGestureRecognizer:tapGesture];
4.设置相关操作:
#pragma mark - 获取头像-(void)ClickHandle{ UIAlertController *AlertSelect = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:_imagePicker animated:YES completion:nil]; }]; UIAlertAction *photo = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:_imagePicker animated:YES completion:nil]; }]; UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [AlertSelect addAction:camera]; [AlertSelect addAction:photo]; [AlertSelect addAction:cancelAction]; [self presentViewController:AlertSelect animated:YES completion:nil]; }- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{ _headShot.image=image;
<pre name="code" class="objc">//将照片存到媒体库 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); [self saveImage:image];[self dismissViewControllerAnimated:YES completion:nil]; }4.存储图片到沙盒
#pragma mark - 照片存到本地后的回调- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{ if (!error) { NSLog(@"存储成功"); } else { NSLog(@"存储失败:%@", error); }}#pragma mark - 保存图片- (void) saveImage:(UIImage *)currentImage { //设置照片的品质 NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5); NSLog(@"%@",NSHomeDirectory()); // 获取沙盒目录 NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/currentImage.png"]; // 将图片写入文件 [imageData writeToFile:filePath atomically:NO]; }6.最后的样式,即使退出程序,图片也不会消失。
1 0
- 【iOS】一个完整的简单的调用系统相机和相册设置头像
- iOS调用系统相册,相机上传头像的基本技巧
- 调用系统相册,相机设置圆形头像
- iOS开发swift如何调用系统相册和相机获取图片设置用户头像
- iOS调用系统相机和相册的详细用法
- iOS学习: 系统相机 相册的调用
- 调用系统的相机和相册
- iOS调用相机和相册 并编辑保存到本地(个人中心设置头像)
- Android头像上传(本地相册和调用系统相机)
- Android通过系统相机和相册设置头像
- iOS开发 调用系统相机和相册
- iOS 调用系统相机和相册
- android调用手机相机、相册,设置头像
- iOS调用相机,相册,上传头像
- iOS 调用相机,获取相册,截取头像
- 调用系统相机相册上传头像
- 调用系统相机/相册,实现更改头像
- 相机和相册的调用
- VBA运行时提示“类range的select方法无效”问题
- MCS51系列单片机实用技术部分课件
- sql server百万数据分页存储过程
- android 6.0编译时出现ERROR:Security problem ,see jack server log
- Android多渠道打包两种方式(带秒速打包)
- 【iOS】一个完整的简单的调用系统相机和相册设置头像
- api对应的安卓版本
- js文件预览
- solr6.2.1 tomcat配置
- python 调用接口造数据
- postman post请求接收不到参数
- linux下安装jdk
- setsockopt()
- Android Retrofit 2.0使用