IOS开发中使用照相机功能的实现
2014-01-08 10:14
621 查看
在程序中使用照相机,或者从相册中选择需要的照片,可以按照以下的步骤实现。
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]];
}
2. 在header里添加以下三个Delegate
UIActionSheetDelegate
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.");
}
}
}
4.UIImagePickerControllerDelegate的实现
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
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];
}
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]];
}
2. 在header里添加以下三个Delegate
UIActionSheetDelegate
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.");
}
}
}
4.UIImagePickerControllerDelegate的实现
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
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开发Swift使用NSURLSessionUploadTask实现后台上传功能
- IOS开发之使用Speex格式实现简单的语音聊天功能(一)
- iOS开发之使用系统库CoreLocation实现定位功能
- 使用UItableview在iOS应用开发中实现好友列表功能
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- 在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
- iOS开发之使用UICollectionView实现美团App的分类功能【偶现大众点评App的一个小bug】
- iOS开发 - Swift使用GCD实现计时器功能
- IOS开发之使用Speex格式实现简单的语音聊天功能(一)
- iOS开发之使用UICollectionView实现美团App的分类功能【偶现大众点评App的一个小bug】
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- iOS开发使用Tab Bar Controller实现页面导航功能
- 在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
- iOS开发 - Swift使用GCD实现计时器功能
- iOS开发之使用XMPPFramework实现即时通信(二)
- iOS开发-清理缓存功能的实现
- C#开发微信门户及应用(40)--使用微信JSAPI实现微信支付功能
- iOS开发-清理缓存功能的实现
- iphone开发--使用ASIHTTPRequest实现下载,断点续传功能
- iOS开发之网络编程--使用NSURLConnection实现大文件下载