ios 照相机和相册的调用
2016-02-29 00:00
381 查看
一个简单的功能,上传照片或者拍照可以用到.
//首先遵循两个代理 <UIImagePickerControllerDelegate,UINavigationControllerDelegate> //我们创建一个btn和一个imageview,btn用来触发事件调起照相机和相册的功能,imageview用来展示选取或者拍摄的图片. self.view.backgroundColor = [UIColor whiteColor]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100 , 100, 200,50)];
btn.backgroundColor = [UIColor blackColor];
[btn setTitle:@"ChoosePhoto" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
_imageview = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
_imageview.backgroundColor = [UIColor lightGrayColor];
_imageview.layer.cornerRadius = CGRectGetHeight(_imageview.bounds)/2;
_imageview.clipsToBounds = YES;
[self.view addSubview:_imageview]; //btn的点击事件 - (void)btnClick:(UIButton *)btn
{ //创建UIAlertController是为了让用户去选择照片来源,拍照或者相册. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0]; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action)
{ //这里可以不写代码 }];
[self presentViewController:alertController animated:YES completion:nil]; //用来判断来源 Xcode中的模拟器是没有拍摄功能的,当用模拟器的时候我们不需要把拍照功能加速 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[alertController addAction:photoAction];
} else {
[alertController addAction:okAction];
[alertController addAction:cancelAction];
}
} //这个是选取完照片后要执行的代理方法 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[picker dismissViewControllerAnimated:YES completion:^{}]; //选取裁剪后的图片 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; /* 此处info 有六个值
* UIImagePickerControllerMediaType; // an NSString UTTypeImage)
* UIImagePickerControllerOriginalImage; // a UIImage 原始图片
* UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片
* UIImagePickerControllerCropRect; // an NSValue (CGRect)
* UIImagePickerControllerMediaURL; // an NSURL
* UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework
* UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo
*/ _imageview.image = image;
}
//首先遵循两个代理 <UIImagePickerControllerDelegate,UINavigationControllerDelegate> //我们创建一个btn和一个imageview,btn用来触发事件调起照相机和相册的功能,imageview用来展示选取或者拍摄的图片. self.view.backgroundColor = [UIColor whiteColor]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100 , 100, 200,50)];
btn.backgroundColor = [UIColor blackColor];
[btn setTitle:@"ChoosePhoto" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
_imageview = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
_imageview.backgroundColor = [UIColor lightGrayColor];
_imageview.layer.cornerRadius = CGRectGetHeight(_imageview.bounds)/2;
_imageview.clipsToBounds = YES;
[self.view addSubview:_imageview]; //btn的点击事件 - (void)btnClick:(UIButton *)btn
{ //创建UIAlertController是为了让用户去选择照片来源,拍照或者相册. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0]; UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}]; UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action)
{ //这里可以不写代码 }];
[self presentViewController:alertController animated:YES completion:nil]; //用来判断来源 Xcode中的模拟器是没有拍摄功能的,当用模拟器的时候我们不需要把拍照功能加速 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[alertController addAction:photoAction];
} else {
[alertController addAction:okAction];
[alertController addAction:cancelAction];
}
} //这个是选取完照片后要执行的代理方法 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[picker dismissViewControllerAnimated:YES completion:^{}]; //选取裁剪后的图片 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; /* 此处info 有六个值
* UIImagePickerControllerMediaType; // an NSString UTTypeImage)
* UIImagePickerControllerOriginalImage; // a UIImage 原始图片
* UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片
* UIImagePickerControllerCropRect; // an NSValue (CGRect)
* UIImagePickerControllerMediaURL; // an NSURL
* UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework
* UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo
*/ _imageview.image = image;
}
相关文章推荐
- iOS -- NSURLSession 实现断点下载
- iOS-自定义图片大小
- ios卡牌游戏项目源码
- 蜂窝教育iOS培训:认真对待每位学员
- IOS tableview
- openstack 中 启动虚拟机出现MP-BIOS bug:8254 timer not connected to IO-APIC
- iOS开发(OC)中的命名规范
- 【iOS】自定义TabBarController
- IOS设计模式-组合设计模式
- iOS GCD使用指南
- IOS 图片点击放大不失真
- IOS 支付宝移动支付接口集成问题
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 此证书的签发者无效Missing iOS Distribution signing identity问题解决
- 不让苹果开发者账号折磨我
- iOS制作自己的静态库(即.a文件)
- ios 粒子开发
- IOS 相册高清图片加载
- ios autolayout代码布局
- iOS CAEmitterLayer 实现粒子效果