【iOS】一个完整的简单的调用系统相机和相册设置头像

1.Xcode8,iOS10的权限设置(不设置会崩溃):

找到项目的info.plist文件,右键open As,以Source Code的形式打开,将以下代码添加进去:

相机权限设置:

NSCameraUsageDescription
    cameraDesciption

相册权限设置:

NSPhotoLibraryUsageDescription
    photoLibraryDesciption

设置好之后,clean一下。


2.在代码中设置代理

@interface MineCtrl ()

@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);
   //不设置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 *)editingInfo
{
    
    _headShot.image=image;
//将照片存到媒体库
    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.最后的样式,即使退出程序,图片也不会消失。

【iOS】一个完整的简单的调用系统相机和相册设置头像_第1张图片




你可能感兴趣的:(【iOS】一个完整的简单的调用系统相机和相册设置头像)