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

来源:互联网 发布:java源代码怎么运行 编辑:程序博客网 时间:2024/05/18 02:45
<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
原创粉丝点击