设置info
ap.h里面
//
// AppDelegate.h
// JHH-调用相机相册
//
// Created by 贾欢 on 2018/5/24.
// Copyright © 2018年 jiahuan. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong) NSPersistentContainer *persistentContainer;
- (void)saveContext;
@end
vc里面
//
// ViewController.m
// JHH-调用相机相册
//
// Created by 贾欢 on 2018/5/24.
// Copyright © 2018年 jiahuan. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong) UIImageView * imageView;
@property(nonatomic,strong) UIButton * selectBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//头像
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, self.view.frame.size.width - 200, self.view.frame.size.width - 200)];
_imageView.backgroundColor = [UIColor lightGrayColor];
_imageView.layer.cornerRadius =(self.view.frame.size.width - 200)/2;
_imageView.layer.masksToBounds = YES;
[self.view addSubview:_imageView];
//按钮
_selectBtn = [[UIButton alloc]initWithFrame:CGRectMake(150, 400, self.view.frame.size.width - 300, 50)];
[_selectBtn setTitle:@"点击选择照片" forState:UIControlStateNormal];
[_selectBtn setBackgroundColor:[UIColor redColor]];
[_selectBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_selectBtn];
}
-(void)btnClick:(UIButton *)btn{
// 创建UIImagePickerController实例
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
// 设置代理
imagePickerController.delegate = self;
// 是否允许编辑(默认为NO)
imagePickerController.allowsEditing = YES;
// 创建一个警告控制器
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// 设置警告响应事件
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 设置照片来源为相机
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
// 设置进入相机时使用前置或后置摄像头
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
// 展示选取照片控制器
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
UIAlertAction *photosAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// 添加警告按钮
[alert addAction:cameraAction];
}
[alert addAction:photosAction];
[alert addAction:cancelAction];
// 展示警告控制器
[self presentViewController:alert animated:YES completion:nil];
}
//设置UIImagePickerController的代理方法:
#pragma mark - UIImagePickerControllerDelegate
// 完成图片的选取后调用的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// 选取完图片后跳转回原控制器
[picker dismissViewControllerAnimated:YES completion:nil];
/* 此处参数 info 是一个字典,下面是字典中的键值 (从相机获取的图片和相册获取的图片时,两者的info值不尽相同)
* UIImagePickerControllerMediaType; // 媒体类型
* UIImagePickerControllerOriginalImage; // 原始图片
* UIImagePickerControllerEditedImage; // 裁剪后图片
* UIImagePickerControllerCropRect; // 图片裁剪区域(CGRect)
* UIImagePickerControllerMediaURL; // 媒体的URL
* UIImagePickerControllerReferenceURL // 原件的URL
* UIImagePickerControllerMediaMetadata // 当数据来源是相机时,此值才有效
*/
// 从info中将图片取出,并加载到imageView当中
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
// 创建保存图像时需要传入的选择器对象(回调方法格式固定)
SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
// 将图像保存到相册(第三个参数需要传入上面格式的选择器对象)
UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, NULL);
}
// 取消选取调用的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
//添加保存图片完成后的回调方法:
// 保存图片后到相册后,回调的相关方法,查看是否保存成功
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error == nil){
NSLog(@"Image was saved successfully.");
} else {
NSLog(@"An error happened while saving the image.");
NSLog(@"Error = %@", error);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end