zoukankan      html  css  js  c++  java
  • IOS-相机、相册

      1 //
      2 //  ViewController.m
      3 //  IOS_0301_相册和相机
      4 //
      5 //  Created by ma c on 16/3/1.
      6 //  Copyright © 2016年 博文科技. All rights reserved.
      7 //
      8 
      9 #import "ViewController.h"
     10 
     11 @interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
     12 
     13 @property (nonatomic, strong) UIImageView *chooseImgView;
     14 @property (nonatomic, strong) UIButton *btnOpenCamera;
     15 
     16 @end
     17 
     18 @implementation ViewController
     19 
     20 - (void)viewDidLoad {
     21     [super viewDidLoad];
     22     
     23     self.chooseImgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
     24     self.chooseImgView.backgroundColor = [UIColor cyanColor];
     25     [self.view addSubview:self.chooseImgView];
     26     self.btnOpenCamera = [UIButton buttonWithType:UIButtonTypeCustom];
     27     [self.btnOpenCamera setFrame:CGRectMake(100, 400, 200, 60)];
     28     [self.btnOpenCamera setBackgroundColor:[UIColor redColor]];
     29     [self.btnOpenCamera setTitle:@"相册相机" forState:UIControlStateNormal];
     30     [self.view addSubview:self.btnOpenCamera];
     31     
     32     [self.btnOpenCamera addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
     33 }
     34 /*
     35  UIImagePickerController(图片拾取器)
     36  能够根据不同的指令,选择开启相机或相册
     37  ps:
     38  1.使用图片拾取器时,实现两个协议
     39    UIImagePickerControllerDelegate
     40    UINavigationControllerDelegate
     41  2.判断当前设备是否存在相机
     42  
     43  */
     44 
     45 - (void)openCamera
     46 {
     47     NSLog(@"打开相册");
     48     
     49     UIActionSheet *actionSheeet = [[UIActionSheet alloc] initWithTitle:@"开启系统相机|相册" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相机" otherButtonTitles:@"相册", nil];
     50     
     51     
     52     [actionSheeet showInView:self.view];
     53     
     54 }
     55 
     56 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
     57 {
     58     switch (buttonIndex) {
     59         case 0:
     60         {
     61             NSLog(@"相机");
     62             //设置图片拾取器
     63             UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
     64             //设置代理
     65             imgPicker.delegate = self;
     66             //设置图片拾取器类型
     67             imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
     68             //是否允许图片编辑
     69             imgPicker.allowsEditing = YES;
     70             //唤醒相机
     71             [self presentViewController:imgPicker animated:YES completion:nil];
     72         }
     73             break;
     74         case 1:
     75         {
     76             NSLog(@"相册");
     77             //设置图片拾取器
     78             UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
     79             //设置代理
     80             imgPicker.delegate = self;
     81             //设置图片拾取器类型
     82             imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     83             //是否允许图片编辑
     84             imgPicker.allowsEditing = YES;
     85             //唤醒相机
     86             [self presentViewController:imgPicker animated:YES completion:nil];
     87 
     88         }
     89             break;
     90             
     91         default:
     92             break;
     93     }
     94     
     95 }
     96 
     97 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
     98 {
     99     //完成获取图片的操作
    100     NSLog(@"%@",info);
    101     //获取编辑后的图片
    102     UIImage *chooseImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    103     NSString *imageName = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
    104     NSLog(@"%@",imageName);
    105     
    106     self.chooseImgView.image = chooseImage;
    107     
    108     
    109     //将图片从系统相册中取出,并保存到沙河中
    110     NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents"];
    111     NSString *realPath = [homePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d",arc4random()%1000]];
    112     
    113     NSLog(@"%@",realPath);
    114     [UIImageJPEGRepresentation(chooseImage, 1.0f) writeToFile:realPath atomically:YES];
    115     
    116     [self dismissViewControllerAnimated:YES completion:nil];
    117     
    118 }
    119 
    120 - (void)didReceiveMemoryWarning {
    121     [super didReceiveMemoryWarning];
    122     // Dispose of any resources that can be recreated.
    123 }
    124 
    125 @end
  • 相关阅读:
    C++类型转换(字符串)
    GDI+ 中Image::FromStream ,用流的方式显示图像
    mfc对话框序列化实例
    配置android开发环境eclipse获取ADT获取不到(转)
    vs开发错误总结
    MFC获取文件操作
    Android系统架构剖析(转)
    OpenCV 图像采样 插值 几何变换
    C++ char*,char[],string,CString转换
    Simscape Multibody 教程 —— 入门学习
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5230250.html
Copyright © 2011-2022 走看看