最近忙于项目,需要拍摄图片,这里先列出部分测试代码。

//
// FirstViewController.m
// UiTest
//
// Created by Tang Huaming on 16/8/13.
// Copyright © 2016年 唐华明. All rights reserved.
// #import "FirstViewController.h"
#import "SecondViewController.h" // 相机应用需要遵守2个协议
@interface FirstViewController () <UIActionSheetDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)goToSecondVC:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (strong, nonatomic) UIImagePickerController *picker;
@property (strong, nonatomic) UIActionSheet *actionSheet; @end @implementation FirstViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib. _picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.allowsEditing = YES; //如果需要对图片进行修改 // UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
// UIImagePickerControllerSourceTypePhotoLibrary //来自图库
// UIImagePickerControllerSourceTypeCamera //来自相机 // 设置源类型之前,需要检查相机是否可用
// if (![self isCameraAvailable]){
// _picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// }else{
// _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// _picker.showsCameraControls = YES; //显示拍照下方的工具栏
// } } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /* 字典info中的键
NSString *const UIImagePickerControllerMediaType;指定用户选择的媒体类型
NSString *const UIImagePickerControllerOriginalImage ;原始图片
NSString *const UIImagePickerControllerEditedImage ;修改后的图片
NSString *const UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const UIImagePickerControllerMediaURL ;媒体的URL
NSString *const UIImagePickerControllerReferenceURL ;原件的URL
NSString *const UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效
}
*/
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"完成照片选择返回------------------>"); UIImage *image = nil;
if ([picker allowsEditing]){
// 获取照片的原图
image = [info objectForKey:UIImagePickerControllerEditedImage];
}else{
// 获得编辑后的图片
image = [info objectForKey:UIImagePickerControllerOriginalImage];
} // 显示图片
[_imageView setImage:image]; // 保存图片到相册
SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, nil); // Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; NSLog(@"Path: %@", jpgPath); // Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
// 可以设置压缩率,数值越小,保存的图片占用空间越小
[UIImageJPEGRepresentation(image, 0.3) writeToFile:jpgPath atomically:YES]; // Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES]; // Let's check to see if files were successfully written... // Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager]; // Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; // Write out the contents of home directory to console
// 返回文件名称的数组
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); // 关闭
[picker dismissViewControllerAnimated:YES completion:^{ }];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消照片选择返回");
[picker dismissViewControllerAnimated:YES completion:^{ }];
} // 保存图片失败时调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo
{
if (error != nil)
{
NSLog(@"Image Can not be saved");
}
else
{
NSLog(@"Successfully saved Image");
}
} // 相机是否可用
- (BOOL)isCameraAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
} // 图库是否可用
- (BOOL) isPhotoLibraryAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
} // 相册是否可用
- (BOOL) isPhotoAlbumAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
} // 前面的摄像头是否可用
- (BOOL)isFrontCameraAvailable{
return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
} // 后面的摄像头是否可用
- (BOOL)isRearCameraAvailable{
return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
} // 判断是否支持某种多媒体类型:拍照,视频
- (BOOL)cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType{
__block BOOL result = NO;
if ([paramMediaType length] == ){
NSLog(@"Media type is empty.");
return NO;
}
NSArray *availableMediaTypes =[UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
[availableMediaTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL*stop) {
NSString *mediaType = (NSString *)obj;
if ([mediaType isEqualToString:paramMediaType]){
result = YES;
*stop= YES;
}
}];
return result;
} - (void)callActionSheetFunc{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照", @"从相册选择", nil,nil];
}else{
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"从相册选择", nil, nil];
} self.actionSheet.tag = ;
[self.actionSheet showInView:self.view];
} // Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet.tag == ) {
NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case :
//来源:相机
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case :
//来源:相册
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
case :
return;
}
}
else {
if (buttonIndex == ) {
return;
} else {
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
// 跳转到相机或相册页面
_picker.sourceType = sourceType; [self presentViewController:_picker animated:YES completion:^{ }];
}
} - (IBAction)goToSecondVC:(id)sender {
[self callActionSheetFunc];
} @end

iOS相机操作笔记的更多相关文章

  1. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  2. IOS学习笔记25—HTTP操作之ASIHTTPRequest

    IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...

  3. IOS学习笔记之关键词@dynamic

    IOS学习笔记之关键词@dynamic @dynamic这个关键词,通常是用不到的. 它与@synthesize的区别在于: 使用@synthesize编译器会确实的产生getter和setter方法 ...

  4. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  5. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  6. iOS开发笔记7:Text、UI交互细节、两个动画效果等

    Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...

  7. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  8. iOS学习笔记之Category

    iOS学习笔记之Category 写在前面 Category是类别(也称为类目或范畴),使用Category,程序员可以为任何已有的类添加方法.使用类别可以对框架提供的类(无法获取源码,不能直接修改) ...

  9. iOS学习笔记之ARC内存管理

    iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...

随机推荐

  1. initrd映像文档的作用和制作

    1.http://pan.baidu.com/s/1dDrGeKL 2.http://wenku.baidu.com/link?url=qPa_jfkEZCbERnwMYWLwm9EZJ_ebMRJA ...

  2. Traveling by Stagecoach 状态压缩裸题

    Traveling by Stagecoach dp[s][v]  从源点到达  v,状态为s,v的最小值.  for循环枚举就行了. #include <iostream> #inclu ...

  3. 1.5.1 Analyzers,Tokenizers,Filters概述

    字段分析器(Analyzers)即用于文档索引也用于查询.一个分析器检查字段的文本,并生成一个token流.分析器可能是一个单独的类,也可能是一系列的tokenizer和filter的组合. 分词器把 ...

  4. SQL Server 2008 表值参数用法

    下面的示例使用 Transact-SQL 并演示如何执行以下操作:创建表值参数类型,声明变量来引用它,填充参数列表,然后将值传递到存储过程. USE AdventureWorks; GO /*创建表值 ...

  5. MySQL(21):事务管理之 事务提交

    1. 现实生活中,许多操作都是需要用户确认的,例如用户删除一个文档,删除时候会弹出一个提示对话框,包含"确认"和"取消".同样的道理,在数据库中有些命令在使用的 ...

  6. 【Linux/Ubuntu学习6】unbuntu 下载android源码

    在Windows下安装Cygwin,通过Cygwin也可在Windows里通过本文的下载步骤下载Android源码. 以下为在Ubuntu下下载Google Android4.4源码的步骤: 1. 安 ...

  7. 【Shell脚本学习3】什么时候使用Shell

    因为Shell似乎是各UNIX系统之间通用的功能,并且经过了POSIX的标准化.因此,Shell脚本只要“用心写”一次,即可应用到很多系统上.因此,之所以要使用Shell脚本是基于: 简单性:Shel ...

  8. extjs 简单入门

    中文网站:http://extjs.org.cn/ 英文网站:http://www.sencha.com/products/extjs/ 1.简介 extJS是一种主要用于创建前端用户界面,是一个基本 ...

  9. [转]Displaying standard DataTables in MVC

    本文转自:http://stackoverflow.com/questions/2243898/displaying-standard-datatables-in-mvc Controller act ...

  10. [转]position:relative leaves an empty space

    本文转自:http://stackoverflow.com/questions/5229081/positionrelative-leaves-an-empty-space 在使用relative进行 ...