IOS-相机相册

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于IOS-相机相册的主要内容,如果未能解决你的问题,请参考以下文章

NR - iOS / Android 相机/相册/日历/定位 等权限 检测+申请代码

NR - iOS / Android 相机/相册/日历/定位 等权限 检测+申请代码

iOS 相机上传图片给服务器

iOS----------调用系统照相机和相册

IOS-相机相册

iOS开发-iOS调用相机调用相册将图片保存到本地相册