forked from IBM-Cloud/openwhisk-visionapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeController.swift
81 lines (68 loc) · 3.06 KB
/
HomeController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import UIKit
/// Controller for the Home page
class HomeController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var versionNumberLabel: UILabel!
@IBOutlet weak var descriptionText: UITextView!
var imageToProcess: UIImage!
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
descriptionText.textContainerInset = UIEdgeInsetsMake(15, 15, 15, 15)
let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
versionNumberLabel.text = "v\(versionNumber) (\(buildNumber))"
}
/// Hides the navigation bar when the view is about to show
override func viewWillAppear(_ animated: Bool) {
navigationController?.setNavigationBarHidden(true, animated: animated)
}
/// Takes a photo with the Camera
@IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
/// Selects a picture from the Photo Library
@IBAction func selectPicture(_ sender: AnyObject) {
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
/// Called when an image has been selected, from the Camera or the Photo Library
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("Retrieving image...")
imagePicker.dismiss(animated: true, completion: nil)
imageToProcess = info[UIImagePickerControllerOriginalImage] as? UIImage
// if this was a picture from the Camera, save it to the Camera Roll
if(picker.sourceType == UIImagePickerControllerSourceType.camera) {
UIImageWriteToSavedPhotosAlbum(imageToProcess, nil, nil, nil)
}
// move to the ResultController
performSegue(withIdentifier: "Result", sender: nil)
}
/// Passes the selected image to the ResultController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "Result") {
let controller = segue.destination as! ResultController
controller.setImage(imageToProcess);
}
}
}