相机拍照,相册照片
来源:互联网 发布:应用统计学大数据方向 编辑:程序博客网 时间:2024/05/01 09:21
首先,我们在头文件中添加需要用到的actionSheet控件,显示图片的UIImageView控件,并且加上所需要的协议
- #import <UIKit/UIKit.h>
- @interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>
- @property (strong, nonatomic) IBOutlet UIImageView *headImage;
- @property (strong, nonatomic) UIActionSheet *actionSheet;
- - (IBAction)clickPickImage:(id)sender;
- @end
通过点击我设置在界面中的按钮来呼出actionSheet控件,来选择相应的操作拍照或是在相册中选择相片,代码如下:
- //
- #import "ImagePickerViewController.h"
- @interface ImagePickerViewController ()
- @end
- @implementation ImagePickerViewController
- @synthesize actionSheet = _actionSheet;
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /**
- @ 调用ActionSheet
- */
- - (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 = 1000;
- [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 == 1000) {
- NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- // 判断是否支持相机
- if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
- switch (buttonIndex) {
- case 0:
- //来源:相机
- sourceType = UIImagePickerControllerSourceTypeCamera;
- break;
- case 1:
- //来源:相册
- sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- break;
- case 2:
- return;
- }
- }
- else {
- if (buttonIndex == 2) {
- return;
- } else {
- sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- }
- }
- // 跳转到相机或相册页面
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
- imagePickerController.delegate = self;
- imagePickerController.allowsEditing = YES;
- imagePickerController.sourceType = sourceType;
- [self presentViewController:imagePickerController animated:YES completion:^{
- }];
- }
- }
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- [picker dismissViewControllerAnimated:YES completion:^{
- }];
- UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
- self.headImage.image = image;
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- - (IBAction)clickPickImage:(id)sender {
- [self callActionSheetFunc];
- }
- @end
///注意点
1.
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
分别表示:图片列表,摄像头,相机相册
2.plist文件配置
3.打开相册,导航条是白色的,看不到取消的itme;2.plist文件配置
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if ([navigationController isKindOfClass:[UIImagePickerController class]])
{
[viewController.navigationController.navigationBar setTintColor:[UIColor blackColor]];
}
}
0 0
- 相机拍照,相册照片
- Android调用相册或相机拍照选取照片并裁剪
- Android调用相册或相机拍照选取照片并裁剪
- Android调用相册或相机拍照选取照片并裁剪
- iOS 相机拍照、相册获取照片(仿微信) 一一 拍照、图片裁剪
- Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁照片
- Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁照片
- Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁、旋转、翻转照片
- iOS 相机拍照、相册获取照片(仿微信) 一一 从相册获取图片、图片裁剪
- 68天-数据库网络---相机调用之读取相册、编辑照片、拍照
- android调用系统相机拍照与打开相册选择照片进行截取保存笔记
- 本地相册选择照片或相机拍照 获取当前Window最上面的 UIViewController
- 开启相机拍照并获取与到照片已经保存到相册中
- 友盟 PushSDK 3.0集成后,应用中调用相机拍照,相册获取照片 程序会 Crash
- android中调用系统的相册或者相机拍照并上传照片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- 关于拍照,相册 取照片
- java.lang.ExceptionInInitializerError springframework.test.context.junit4.springJunit4ClassRunner.<>
- linux下安装nginx
- java 中往jframe中添加组件是添加到哪里了?
- String和byte之家的转换:中文的处理
- 初学C
- 相机拍照,相册照片
- 数据库自增id没有从0开始
- PHP JSON_ENCODE 不转义中文汉字的方法
- bzoj2333: [SCOI2011]棘手的操作(启发式合并做法)
- 20170505 大数据量下高并发同步的讲解(不看,保证你后悔)
- CSS中margin和padding的区别
- C++第4次实验(基础班)—循环结构程序设计(参考答案)-项目7-3:三色球问题
- 匿名函数的this指向
- 可视化编程