-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostViewController.swift
More file actions
92 lines (76 loc) · 3.44 KB
/
PostViewController.swift
File metadata and controls
92 lines (76 loc) · 3.44 KB
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
82
83
84
85
86
87
88
89
90
91
92
//
// PostViewController.swift
//
//
// Created by Austin Evans on 1/19/19.
//
import UIKit
class PostViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var selectedImage: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Comfortaa", size: 30)!]
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SubmissionFormIdentifier",
let image = selectedImage {
let destiation = segue.destination as! PostSubmitViewController
destiation.newImage = image
}
}
@IBAction func photoLibraryTapped(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = .photoLibrary
self.present(pickerController, animated: true, completion: nil)
}
}
@IBAction func cameraTapped(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = .camera
self.present(pickerController, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
dismiss(animated: true, completion: nil)
if let image = info[.originalImage] as? UIImage {
selectedImage = cropToBounds(image: image, width: 400, height: 400)
}
performSegue(withIdentifier: "SubmissionFormIdentifier", sender: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
// https://stackoverflow.com/questions/32041420/cropping-image-with-swift-and-put-it-on-center-position
func cropToBounds(image: UIImage, width: Double, height: Double) -> UIImage {
let cgimage = image.cgImage!
let contextImage: UIImage = UIImage(cgImage: cgimage)
let contextSize: CGSize = contextImage.size
var posX: CGFloat = 0.0
var posY: CGFloat = 0.0
var cgwidth: CGFloat = CGFloat(width)
var cgheight: CGFloat = CGFloat(height)
// See what size is longer and create the center off of that
if contextSize.width > contextSize.height {
posX = ((contextSize.width - contextSize.height) / 2)
posY = 0
cgwidth = contextSize.height
cgheight = contextSize.height
} else {
posX = 0
posY = ((contextSize.height - contextSize.width) / 2)
cgwidth = contextSize.width
cgheight = contextSize.width
}
let rect: CGRect = CGRect(x: posX, y: posY, width: cgwidth, height: cgheight)
// Create bitmap image from context using the rect
let imageRef: CGImage = cgimage.cropping(to: rect)!
// Create a new image based on the imageRef and rotate back to the original orientation
let image: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)
return image
}
}