ios 照相机和相册的调用(ios15.6)

网友投稿 322 2022-06-15


一个简单的功能,上传照片或者拍照可以用到.

//首先遵循两个代理 //我们创建一个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 *)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;

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Lambda表达式的意义(Lambda 表达式)
下一篇:iOS绘图(ios绘图软件免费)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~