通讯录——选择图片

//
//  ViewController.m
//  Photo
//
//  Created by lanou on 16/1/27.
//  Copyright (c) 2016年 lanou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (strong, nonatomic) IBOutlet UIButton *photoBtn;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController
- (IBAction)click:(id)sender {
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提醒" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //判断设备是否存在摄像头,有,调用系统摄像头,没有,提醒用户
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            //创建相机
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            //指定数据来源来自于相机
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            //指定代理
            picker.delegate = self;
            //允许编辑
            picker.allowsEditing = YES;
            //模态弹出相机
            [self presentViewController:picker animated:YES completion:nil];
            
        }else{//没有摄像头,提醒用户
            UIAlertController *alter1 = [UIAlertController alertControllerWithTitle:@"您的设备没有摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alter1 addAction:action1];
            //模态弹出 UIAlertController
            [self presentViewController:alter1 animated:YES completion:nil];
        }
    }];
    [alter addAction:action];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //指定数据来源于相册
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }];
    [alter addAction:action2];
    //模态弹出 UIAlertController
    [self presentViewController:alter animated:YES completion:nil];
}
//选取图片后执行的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"%@", info);
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.imageView.image = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
原文地址:https://www.cnblogs.com/chongyu/p/5209415.html