您的位置:首页 > 移动开发 > IOS开发

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: