iOS相机操作笔记
最近忙于项目,需要拍摄图片,这里先列出部分测试代码。
//
// 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相机操作笔记的更多相关文章
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
- IOS学习笔记25—HTTP操作之ASIHTTPRequest
IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...
- IOS学习笔记之关键词@dynamic
IOS学习笔记之关键词@dynamic @dynamic这个关键词,通常是用不到的. 它与@synthesize的区别在于: 使用@synthesize编译器会确实的产生getter和setter方法 ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记10-UIView动画
上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...
- iOS开发笔记7:Text、UI交互细节、两个动画效果等
Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- iOS学习笔记之Category
iOS学习笔记之Category 写在前面 Category是类别(也称为类目或范畴),使用Category,程序员可以为任何已有的类添加方法.使用类别可以对框架提供的类(无法获取源码,不能直接修改) ...
- iOS学习笔记之ARC内存管理
iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...
随机推荐
- Ubuntu上手动安装nginx
最近需要利用nginx上搭建一个网站,因此在自己的电脑上安装了nginx,现在分享一下自己在安装过程及遇到的问题. 1.下载需要的nginx版本的安装包. axel -n http://nginx.o ...
- 1.7.7 Spell Checking -拼写检查
1. SpellCheck SpellCheck组件设计的目的是基于其他,相似,terms来提供内联查询建议.这些建议的依据可以是solr字段中的terms,外部可以创建文本文件, 或者其实lucen ...
- PHP常用文件函数和目录函数整理
一.常用文件函数库 1.basename(); -- 返回路径中的文件名部分. string basename ( string $path [, string $suffix ] ) //给出一个包 ...
- Linux系统cpu 100%修复案例
Linux系统cpu 100%修复案例 阿里云技术支持团队:完颜镇江 案例背景: Linux主机连续三天CPU% 处理思路: 1. 登录服务器查看/var/log/messages+/var/lo ...
- JQuery POST请求乱码...
引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何解决呢? 问题的引入: var regid = $('#oregion').combobox('getV ...
- java jdbc连接mysql
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- C中宏展开问题
C中宏展开问题 简单记录一下碰到的问题. #define STR(x) #x 我们知道使用上面的宏可以将x转换为字符串"x". 但是如果这样用: #define NUM 3 #de ...
- 跨越跳板机传文件nc
从线上服务器与本机互传文件 传输方 nc -l 10000 < a.tar 接收方 nc xx.xx.xx.xx 10000 >a.tar 原理: 文件传输方运行nc,指定端口,设置监听文 ...
- selenium遍历控件集合
场景:需要重复增加地址栏信息,如果地址信息超过了5个就不开始增加 如图: 1.找到控件集合,在遍历每个子元素,在进行选择 1.先找到最外层的div的控件集合 2.外层的css定位为: int star ...
- xcode6 真机运行报错 Command /usr/bin/codesign failed with exit code 1
解决方法: 百度下载‘iphone配置实用工具’, 打开此软件之后,选择预配置描述文件, 选中iphone过期和重复的描述文件,按delete键删除.然后重启xcode即可