[Xcode 實際操做]9、實用進階-(13)調用相機並獲取拍攝後的圖片

目錄:[Swift]Xcode實際操做html

本文將演示如何調用相機並獲取拍攝後的圖片。swift

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】ide

 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 }

須要使用真實設備進行調試。post

相關文章
相關標籤/搜索