ios|ios 拍照功能简单实现
文章图片
storyboard.png import
{
UIImagePickerController * _imagePickerController;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
/*创建对象/
[super viewDidLoad];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
btn.backgroundColor = [UIColor purpleColor];
[btn addTarget:self action:@selector(gotoPhoto) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}
{
NSLog(@"photo");
self.imageView.backgroundColor = [UIColor yellowColor];
[self setupImagePickerController];
}
pragma mark --代理方法UIImagePickerControllerDelegate--
{
//第一步:判断摄像头是否打开
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//第二步:实例化UIImagePickerController对象
_imagePickerController = [[UIImagePickerController alloc] init];
//第三步:告诉picker对象是获取相机资源
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//第四步:设置代理
_imagePickerController.delegate = self;
//第五步:设置picker可以编辑
_imagePickerController.allowsEditing = YES;
//第六步:设置进去的模态方式
_imagePickerController.modalPresentationStyle=UIModalPresentationOverCurrentContext;
//第七步:跳转
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
}
pragma mark --取消选择图片
{
//获取选中资源的类型
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//只能拍照(还有摄像或者二者都可以有的)
NSString *requiredMediaType = (NSString *)kUTTypeImage;
if([mediaType isEqualToString:requiredMediaType])
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
self.imageView.image = image;
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
推荐阅读
- 拍照一年啦,如果你想了解我,那就请先看看这篇文章
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 分享!如何分分钟实现微信扫二维码调用外部浏览器打开指定页面的功能
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- Excel|Excel 2013 新增功能之瞬间填充整列数据!
- 接口|axios接口报错-参数类型错误解决