[Xcode10 实际操作]九、实用进阶-(13)调用相机并获取拍摄后的图片

本文将演示如何调用相机并获取拍摄后的图片。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

 1 import UIKit
 2 
 3 //首先添加两个协议 UIImagePickerControllerDelegate, UINavigationControllerDelegate
 4 //来实现打开相机并拍照的功能
 5 class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
 6 
 7     //添加一个图像视图属性,用来显示从相机设备中读取的照片
 8     var imageView: UIImageView!
 9     //添加一个图片拾取控制器,作为当前视图控制器的属性
10     var imagePickerController: UIImagePickerController!
11     
12     override func viewDidLoad() {
13         super.viewDidLoad()
14         // Do any additional setup after loading the view, typically from a nib.
15         
16         //初始化图像视图,并设置其位置在(20,120),尺寸为(280,200)。
17         self.imageView = UIImageView(frame: CGRect(x: 20, y: 120, width: 280, height: 200))
18         //然后将图像视图,添加到当前视图控制器的根视图。
19         self.view.addSubview(imageView)
20         
21         //创建一个按钮控件,并设置其位置在(20,60),尺寸为(280,40)
22         let button = UIButton(frame: CGRect(x: 20, y: 60, width: 280, height: 40))
23         //同时设置按钮在正常状态下的标题文字。
24         button.setTitle("Shot", for: .normal)
25         //然后给按钮绑定点击事件
26         button.addTarget(self, action: #selector(ViewController.pickImage), for: UIControl.Event.touchUpInside)
27         //设置按钮的背景颜色为深灰色
28         button.backgroundColor = UIColor.darkGray
29         
30         //同样将按钮,添加到当前视图控制器的根视图
31         self.view.addSubview(button)
32     }
33     
34      //添加一个方法,用来响应按钮的点击事件
35     @objc func pickImage()
36     {
37         //首先检测相机设备是否可以正常使用
38         if(UIImagePickerController.isSourceTypeAvailable(.camera))
39         {
40             //初始化图片拾取控制器对象
41             self.imagePickerController = UIImagePickerController()
42             //设置图片拾取控制器的代理对象,为当前的视图控制器
43             self.imagePickerController.delegate = self
44             //设置图片拾取控制器,是否允许用户移动、缩放和剪切图片
45             self.imagePickerController.allowsEditing = true
46             //设置图片拾取控制器的来源类型为相机设备
47             self.imagePickerController.sourceType = UIImagePickerController.SourceType.camera
48             //最后在当前视图控制器窗口,展示图片拾取控制器。
49             self.present(self.imagePickerController, animated: true, completion: nil)
50         } 
51     }
52     
53     //添加一个代理方法,用来响应完成图片拾取的事件
54     func imagePickerController(_ picker: UIImagePickerController, 
55          didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
56         //将用户选择的图片,赋予图像视图                          
57         self.imageView.image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
58         //然后取消图片拾取控制器的展示
59         self.dismiss(animated: true, completion: nil)
60     }
61 
62     //添加一个代理方法,用来响应用户取消图片拾取的事件
63     func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
64     {
65         //当用户取消图片拾取时,隐藏图片拾取控制器
66         self.dismiss(animated: true, completion: nil)
67     }
68     
69     override func didReceiveMemoryWarning() {
70         super.didReceiveMemoryWarning()
71         // Dispose of any resources that can be recreated.
72     }
73 }

需要使用真实设备进行调试。

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10089144.html