iOS在更改用户头像并保存至本地沙盒目录中对于UIImagePickerController、UIAlertController的使用方法

感谢总结的方法

.h

#import 

//获取系统版本号
#define IS_iOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? YES : NO)
//获取屏幕尺寸
#define SW [UIScreen mainScreen].bounds.size.width
#define SH [UIScreen mainScreen].bounds.size.height
@interface ViewController : UIViewController
@property (nonatomic, strong)UIButton *iconBtn;
@end

.m


- (void)viewDidLoad {
    [super viewDidLoad];

    //初始化Button
    _iconBtn = [[UIButton alloc]initWithFrame:CGRectMake((SW - 100)/2,(SH / 3) - 100,100,100)];
    _iconBtn.backgroundColor =[UIColor redColor];

    //加载首先访问本地沙盒是否存在相关图片
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];    
    UIImage *savedImage = [UIImage imageWithContentsOfFile:fullPath];

    if (!savedImage)
    {
        //默认头像
        [_iconBtn setImage:[UIImage imageNamed:@"head"] forState:UIControlStateNormal];
    }
    else
    {
        [_iconBtn setImage:savedImage forState:UIControlStateNormal];
    }

    _iconBtn.layer.masksToBounds = YES;

    _iconBtn.layer.cornerRadius = _iconBtn.frame.size.height / 2;

    [_iconBtn addTarget:self action:@selector(changeIcon) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_iconBtn];

}


- (void)changeIcon
{
    UIAlertController *alertController;

    __block NSUInteger blockSourceType = 0;

    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        //支持访问相机与相册情况
        alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

            imagePickerController.delegate = self;

            imagePickerController.allowsEditing = YES;

            imagePickerController.sourceType = blockSourceType;

            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];

        [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击拍照");
            //相机
            blockSourceType = UIImagePickerControllerSourceTypeCamera;

            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

            imagePickerController.delegate = self;

            imagePickerController.allowsEditing = YES;

            imagePickerController.sourceType = blockSourceType;

            [self presentViewController:imagePickerController animated:YES completion:nil];
        }]];

        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击取消");
            // 取消
            return;
        }]];

        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        //只支持访问相册情况
        alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击从相册中选取");
            //相册
            blockSourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

            imagePickerController.delegate = self;

            imagePickerController.allowsEditing = YES;

            imagePickerController.sourceType = blockSourceType;

            [self presentViewController:imagePickerController animated:YES completion:^{

            }];
        }]];

        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"点击取消");
            // 取消
            return;
        }]];

        [self presentViewController:alertController animated:YES completion:nil];
    }
}


#pragma mark - 选择图片后,回调选择

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];

    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

    /* 此处info 有六个可选类型
     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
     * UIImagePickerControllerMediaURL;       // an NSURL
     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
     */

    [_iconBtn setImage:image forState:UIControlStateNormal];

    [self saveImage: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];
}

你可能感兴趣的:(iOS在更改用户头像并保存至本地沙盒目录中对于UIImagePickerController、UIAlertController的使用方法)