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

.h

#import <UIKit/UIKit.h>

//获取系统版本号
#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<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>
@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];
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值