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

//
// 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. 转:《JavaScript—之对象参数的引用传递》

    转自:博客园 Wayou http://www.cnblogs.com/Wayou/p/javascript_arguments_passing_with_reference.html 变量 1.Ja ...

  2. C# 自定义重绘DataGridView

    using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using Syste ...

  3. 如何删除Weblogic域

    1. delete entry in WL_HOME/common/nodemanager/nodemanager.domains 2. delete entry in FMW_HOME/domain ...

  4. Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2899   Accepted: 133 ...

  5. C++重载自增/减操作符

    作为类成员使用. 前缀是先加/减1,再取值:后缀是先取值,再加/减1. 前缀是左值,返回引用:后缀是右值,返回值. 后缀多一个int参数进行区分,用时编译器会传个没用的0作实参. 在后缀实现中调用前缀 ...

  6. [原创] Web UI自动化应用测试框架实践 - 概览

    之前为我们部门做的一个UI框架.不能纯粹解读为框架,主要是做了一些简单的分层设计,以解决稳定性.降低复杂性.提升可维护性以及快速构建测试用例等实际问题. 主要部分:1. 测试数据.主要提供测试类库需要 ...

  7. Oracle基础 动态SQL语句

    一.静态SQL和动态SQL的概念. 1.静态SQL 静态SQL是我们常用的使用SQL语句的方式,就是编写PL/SQL时,SQL语句已经编写好了.因为静态SQL是在编写程序时就确定了,我们只能使用SQL ...

  8. POJ 2253 Frogger (最短路)

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28333   Accepted: 9208 Descript ...

  9. GDB使用

    1.display val 设置显示格式 2.i b显示所有断点

  10. Backbone.js学习之Router

    官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...