IOS 使用照相机和图片库
在程序中使用照相机,或者从相册中选择需要的照片,可以按照以下的步骤实现。
1.生成一个UIImagePickerController对象
2.用presentModalViewController来显示它
3.实现UIImagePickerControllerDelegate的方法
举个例子,此例子的功能如下:
点击一个照相按钮,弹出一个ActionSheet让用户选择是从相册选择照片还是用相机新照一张照片。
代码如下:
1.点击照相按钮后弹出ActionSheet
//Show Action Sheet: 1. Take Photo 2. Select Photo From Album
UIActionSheet *photoBtnActionSheet =
[ [UIActionSheet alloc ] initWithTitle :nil
delegate :self
cancelButtonTitle :@ "Cancel"
destructiveButtonTitle :nil
otherButtonTitles :@ "Photo Library" ,@ "Take Photo" , nil ] ;
[photoBtnActionSheet setActionSheetStyle :UIActionSheetStyleBlackOpaque ] ;
[photoBtnActionSheet showInView : [self. view window ] ] ;
}
2. 在header里添加以下三个Delegate
UIImagePickerControllerDelegate
UINavigationControllerDelegate
3.UIActionSheetDelegate的实现
NSLog (@ "Action Sheet Button Index: %d" ,buttonIndex ) ;
if (buttonIndex == 0 ) {
//Show Photo Library
@try {
if ( [UIImagePickerController isSourceTypeAvailable :UIImagePickerControllerSourceTypeSavedPhotosAlbum ] ) {
UIImagePickerController *imgPickerVC = [ [UIImagePickerController alloc ] init ] ;
[imgPickerVC setSourceType :UIImagePickerControllerSourceTypePhotoLibrary ] ;
[imgPickerVC. navigationBar setBarStyle :UIBarStyleBlack ] ;
[imgPickerVC setDelegate :self ] ;
[imgPickerVC setAllowsEditing :NO ] ;
//显示Image Picker
[self presentModalViewController :imgPickerVC animated :NO ] ;
} else {
NSLog (@ "Album is not available." ) ;
}
}
@catch (NSException *exception ) {
//Error
NSLog (@ "Album is not available." ) ;
}
}
if (buttonIndex == 1 ) {
//Take Photo with Camera
@try {
if ( [UIImagePickerController isSourceTypeAvailable :UIImagePickerControllerSourceTypeCamera ] ) {
UIImagePickerController *cameraVC = [ [UIImagePickerController alloc ] init ] ;
[cameraVC setSourceType :UIImagePickerControllerSourceTypeCamera ] ;
[cameraVC. navigationBar setBarStyle :UIBarStyleBlack ] ;
[cameraVC setDelegate :self ] ;
[cameraVC setAllowsEditing :NO ] ;
//显示Camera VC
[self presentModalViewController :cameraVC animated :NO ] ;
} else {
NSLog (@ "Camera is not available." ) ;
}
}
@catch (NSException *exception ) {
NSLog (@ "Camera is not available." ) ;
}
}
}
4.UIImagePickerControllerDelegate的实现
NSLog (@ "Image Picker Controller canceled." ) ;
//Cancel以后将ImagePicker删除
[self dismissModalViewControllerAnimated :NO ] ;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"Image Picker Controller did finish picking media.");
//TODO:选择照片或者照相完成以后的处理
[self dismissModalViewControllerAnimated:NO];
}
IOS 使用照相机和图片库相关推荐
- 调用照相机与图片库并实现保存图片到图片库
传智播客ios7-仿照微信发送图片调用照相机与图片库并实现保存图片到图片库 #import <UIKit/UIKit.h>// 添加委托 @interface ViewController ...
- 【Xamarin】MonoTouch - iOS 使用 UIImagePickerController 打开图片库和相机选择图片修改头像...
Application tried to present modally an active controller <UIImagePickerController: 0x7b6ff400> ...
- iOS Camera照相机
iOS 5 取景器和实时滤镜的制作 1.使用UIImagePickerController捕捉媒体制作取景器 UIImagePickerController是一个UINavigationControl ...
- IOS使用照相机的功能
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ ...
- iOS打开照相机与本地相册选择图片
最近正好项目里面要集成"打开照相机与本地相册选择图片"的功能,今天就在这边给大家写一个演示程序:打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上.好了废话不多说,因为比 ...
- iOS 打开照相机与本地相册选择图片
最近正好项目里面要集成"打开照相机与本地相册选择图片"的功能,今天就在这边给大家写一个演示程序:打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上.好了废话不多说,因为比 ...
- iOS 获取照相机实时预览图片
使用Objective-C实现 AVController.h头文件 #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundatio ...
- UIImagePicketView(照相机类)的使用
1.挂代理 UIImagePickerControllerDelegate,UINavigationControllerDelegate 2. 看代码 UIImagePickerController ...
- Cordova使用入门简介入门教程
什么是cordova? 目录 什么是cordova? 历史和对比 cordova有什么? cordova支持系统 安装 基本命令 1.创建项目 2.添加平台 3.运行app 注意事项 cordova插 ...
最新文章
- 第八章 让开发板发出声音:蜂鸣器驱动读后感
- Linux下常用配置文件及常用命令介绍
- 13_python基础—函数进阶(参数、返回值、递归)
- tinyint占几个字节_随笔几个小问题
- c语言 屏幕亮度调节_好手机的屏幕有什么不同?我们为何需要一块好屏幕?
- 手机4k屏幕测试软件,手机4K屏、2K屏、1080p屏对比测试!惊叹
- SolidWorks2020绘制电脑折叠支架
- java 动态性之反射机制 详解 案例,mybatis字段映射原理
- python数据拟合之scipy.optimize.curve_fit
- 星星之火-6:无线通信必须知道的“信号”与傅里叶信号变换
- robots文件的优化
- 全国计算机一等奖说课视频,全国一等奖说课稿体育视频
- Java接口的基本概念详解
- 苹果cms利用定时任务生成静态执行操作
- CAN BLF包解析和播包发送(一):CAN dbc解析
- 论文阅读-ViTDet:Exploring Plain Vision Transformer Backbones for Object Detection
- Flink中任务的并发:slot和parallelism
- ListView数据更新UI不刷新
- 获取电脑硬件对应的驱动程序信息
- 苹果系列机型专业刷机,解锁,解ID