系统自带照相

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];

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值