IOS开发中使用照相机功能的实现
来源:互联网 发布:淘宝代理商品怎么发货 编辑:程序博客网 时间:2024/04/29 18:23
在程序中使用照相机,或者从相册中选择需要的照片,可以按照以下的步骤实现。
1.生成一个UIImagePickerController对象
2.用presentModalViewController来显示它
3.实现UIImagePickerControllerDelegate的方法
举个例子,此例子的功能如下:
点击一个照相按钮,弹出一个ActionSheet让用户选择是从相册选择照片还是用相机新照一张照片。
代码如下:
1.点击照相按钮后弹出ActionSheet
- (void)takePhotoBtnTapped:(UIButton *)sender {
//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]];
}
//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
UIActionSheetDelegate
UIImagePickerControllerDelegate
UINavigationControllerDelegate
UIImagePickerControllerDelegate
UINavigationControllerDelegate
3.UIActionSheetDelegate的实现
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
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.");
}
}
}
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的实现
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"Image Picker Controller canceled.");
//Cancel以后将ImagePicker删除
[self dismissModalViewControllerAnimated:NO];
}
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];
}
0 0
- IOS开发中使用照相机功能的实现
- IOS使用照相机的功能
- 使用照相机的功能
- Android开发中使用照相机功能 及其常见问题解法方法
- Android中照相机的使用
- 海思开发板wince平台实现照相机功能
- Android开发应用之照相机的使用
- IOS 调用照相机、相册功能
- iOS开发——定制圆形头像与照相机图库的使用
- ios中录音功能的实现AudioSession的使用
- iOS开发中实现后台定位功能
- 用gstreamer实现照相机功能
- 用gstreamer实现照相机功能
- 用gstreamer实现照相机功能
- IOS开发 照相机小结一
- 面向照相机的开发
- iOS开发相册和照相机打不开的时候
- 实现简单的照相机
- ARM 指令集版本和ARM 版本
- 信息图:iOS 7开发者需要知道的事
- 排序算法和递归算法
- Oracle dbms_random
- 安装Java在Windows和Linux下的开发环境
- IOS开发中使用照相机功能的实现
- OSG基本几何体的透明效果和网格模型
- 2014年1月7日Google实习电面
- 学习STM8的好文章 .
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- JAVA计划任务
- AR应收
- Oracle_Day2 查询练习,以及分页
- openCV中IplImage的使用