zoukankan      html  css  js  c++  java
  • 通讯录——选择图片

    //
    //  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
  • 相关阅读:
    Diablo3 英雄榜-显示用户的装备信息-Volley读取API的图片资源,使用MySingleton管理RequestQueue
    Diablo3英雄榜-使用Volley和Gson来处理暴雪API的Json数据
    Diablo3英雄榜-API分析
    《Android编程权威指南》-读书笔记(十一) 完善CriminalIntent
    《Android编程权威指南》-读书笔记(十)-从一个内涵段子开始第二个例子
    さらば、博客园よ
    Kotlin有点用力过猛了
    记一个耻辱
    备份一个省市区JSON数据
    博(wǒ)客(zì)园(jǐ)的排版真是丑毙了
  • 原文地址:https://www.cnblogs.com/chongyu/p/5209415.html
Copyright © 2011-2022 走看看