获取系统相册,并保存文件

<span style="font-size:18px;">#import "ViewController.h"

@interface ViewController ()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _icon.layer.cornerRadius = _icon.frame.size.width / 2;
}
- (IBAction)getPhoto:(id)sender {
    
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"是否打开相机" message:@"打开" preferredStyle:UIAlertControllerStyleActionSheet];
    //判断是否有相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIAlertAction *alt = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            //相机
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.delegate = self;
            imagePickerController.allowsEditing = YES;
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }];
        [alter addAction:alt];
    
    }
    
    //打开相册
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *picController = [[UIImagePickerController alloc] init];
        picController.delegate = self;
        picController.allowsEditing = YES;
        //跳转到相册页面
 <span style="color:#FF0000;">       picController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picController animated:YES completion:nil];</span>
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alter addAction:action];
    [alter addAction:cancel];
    
    [self presentViewController:alter animated:YES completion:nil];
}

//保存到沙盒文件
- (void)savePhoto:(UIImage *)image withName:(NSString *)name{
    
    NSData *data = UIImageJPEGRepresentation(image, 1);
    
    //获取沙盒目录
    NSString *path = [[NSHomeDirectory() stringByAppendingString:@"Documents"]stringByAppendingString:name];
    
    NSLog(@"%@", path);
    [data writeToFile:path atomically:YES];
    
}

//选中图片以后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //保存到沙盒
    [self savePhoto:image withName:@"保存的图片.jpg"];
    
    NSString *path = [[NSHomeDirectory() stringByAppendingString:@"Documents"] stringByAppendingString:@"保存的图片.jpg"];
    
    UIImage *saveImage = [[UIImage alloc] initWithContentsOfFile:path];
    
    [_icon setImage:saveImage];
    [_imageView setImage:saveImage];
}

//选择取消后的代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}</span>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值