IOS 使用照相机和图片库
来源:互联网 发布:mac文件复制到移动硬盘 编辑:程序博客网 时间:2024/04/28 12:54
在程序中使用照相机,或者从相册中选择需要的照片,可以按照以下的步骤实现。
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];
}
- IOS 使用照相机和图片库
- 照相机和图片库(1)
- 照相机和图片库(2)
- Swift学习笔记(4)使用UIImagePickerController实现从设备图片库和照相机获取图片
- iOS相机, 相册, 图片库 和 UIAlertController的使用
- 上传头像(照相机、图片库)
- IOS使用照相机的功能
- ios 使用UIImagePickerController 打开图片库和相机选择图片修改头像(iphone版本)
- ios 使用UIImagePickerController 打开图片库和相机选择图片修改头像(iphone版本)
- ios 使用UIImagePickerController 打开图片库和相机选择图片修改头像
- 调用照相机与图片库并实现保存图片到图片库
- IOS调用照相机和相册功能
- iOS 打开iPhone照相机和相册
- iOS调用系统照相机和闪光灯功能
- iOS ---打开iPhone照相机和相册
- iOS如何调用照相机和相册
- Unity Android/IOS 打开图片库和相机,并加载图片
- 使用照相机
- 利用Python自带的包可以建立简单的web服务器
- Remove Element
- 使用ADO的方法操作EXCEL文件
- 警惕分页中的checkbox
- HTTP协议和HTTPS协议详解
- IOS 使用照相机和图片库
- socket的常见问题---《六》
- 可执行文件自删除大法步步高
- DataTable转换成IList
- STM32 USB IAP 步骤
- Spring+Struts继承(方案一)
- oracle 9i 10g 11g 升级 patch 补丁号对照表
- usaco 2.3 money
- 第三章课后第三题