iOS 获取图片有三种方法:
1. 直接调用摄像头拍照
2. 从相册中选择
3. 从图库中选择
UIImagePickerController 是系统提供的用来获取图片和视频的接口;
用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:
1. 初始化UIImagePickerController 类;
2. 设置UIImagePickerController 实例的数据来源类型(下面解释);
3. 设置设置代理;
4. 如果需要做图片修改的话设置allowsEditing =yes。
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong) UIImageView *imageview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setButton];
[self setimageView];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)setButton
{
UIButton *button = [[UIButton alloc]init];
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"设置图片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(10, 60, 50, 30);
[self.view addSubview:button];
_button = button;
}
-(void)buttonAction
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"照相机",@"相册",@"图库", nil];
[actionSheet showInView:self.view];
}
-(void)setimageView
{
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(10, 80, 200, 300);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
_imageview = imageView;
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
{
NSError *error;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//创建输入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
//判断相机是否可用
UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:@"无法使用相机" message:@"请在iPhone的\"设置-隐私-相机\"中允许访问相机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alerView show];
}else
{
//打开相机
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = sourceType;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
}
break;
case 1:
{
//打开相册
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = sourceType;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
break;
case 2:
{
//打开图库
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = sourceType;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
break;
default:
break;
}
}
//获取图片代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSLog(@"%@",info);
_imageview.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self imagePickerControllerDidCancel:picker];
}
//取消代理
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}