ios 照相机和相册的调用
2018-07-20 来源:编程学习网
一个简单的功能,上传照片或者拍照可以用到.
//首先遵循两个代理 <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;
}
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:iOS 内存管理
下一篇:关于App程序员泡沫
最新资讯
热门推荐