(I am a beginner) I am using UIKit, right now my app can upload an image from the gallery/camera. I’m trying to let the user draw pins onto the image. I know I have to use UIView for drawing to work, but I am having a hard time find a helpful tutorial for it on going about this. Anything helps, thanks :’).
import UIKit
class NewProjectViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
@IBOutlet var imageView: UIImageView!
@IBOutlet weak var projectSave: UIButton!
@IBOutlet weak var projectName: UITextField!
var image: UIImage = UIImage()
var saved = false
var _projectName: String = ""
override func viewDidLoad() {
super.viewDidLoad()
initializeHideKeyboard()
projectName.delegate = self
}
/* Uploads the photogallery and allows user select image and zoom in/out*/
@IBAction func onClickPickImage() {
_projectName = projectName.text!
if (_projectName != "")
{
let vc = UIImagePickerController()
// Suggested to use PHPicker
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
saved = false
} else
{
print("Name the project first before uploading an image")
}
}
@IBAction func onClickCamera(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
_projectName = projectName.text!
if (_projectName != "")
{
let vc = UIImagePickerController()
vc.delegate = self
vc.sourceType = .camera;
vc.allowsEditing = true
present(vc, animated: true, completion: nil)
saved = false
} else
{
print("Name the project first before uploading an image")
}
}
}
/* saveButtonPressed doesn't save yet, but loads the (uncompleted) 'SavedProjects' view and prints to the counsole of what was saved in File*/
@IBAction func saveButtonPressed(_ sender: UIButton) {
_projectName = projectName.text!
if (_projectName != "" && saved == false){
//FIXME: When calling savePng() it doesn't work at all within here, need to figure out how to get image from imagePickerController()
let success: () = savePng(image: image, imageName: "\(_projectName).png")
print("Here \(success)")
print(_projectName)
saved = true
} else if (_projectName != "" && saved == true){
print("Project is already saved!")
} else if (_projectName == ""){
print("Name this project!")
}
}
func initializeHideKeyboard(){
//Declare a Tap Gesture Recognizer which will trigger our dismissMyKeyboard() function
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(dismissMyKeyboard))
//Add this tap gesture recognizer to the parent view
view.addGestureRecognizer(tap)
}
@objc func dismissMyKeyboard(){
//endEditing causes the view (or one of its embedded text fields) to resign the first responder status.
//In short- Dismiss the active keyboard.
view.endEditing(true)
}
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
imageView.image = image
}
picker.dismiss(animated: true, completion: nil)
}
@objc func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func documentDirectoryPath() -> URL? {
let path = FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask)
return path.first
}
/* Currently saves UIImage with the filename of the directory path + "\(_projectName).png" */
func savePng(image: UIImage, imageName: String) {
let fileManager = FileManager.default
let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("\(_projectName).png")
if !fileManager.fileExists(atPath: path) {
try! fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
}
let url = NSURL(string: path)
let imagePath = url!.appendingPathComponent(imageName)
let urlString: String = imagePath!.absoluteString
//let imageData = UIImageJPEGRepresentation(image, 0.5)
let imageData = image.pngData()
fileManager.createFile(atPath: urlString as String, contents: imageData, attributes: nil)
}
func readImage() {
if let path = documentDirectoryPath() {
let pngImageURL = path.appendingPathComponent("\(_projectName).png")
print(pngImageURL)
let pngImage = FileManager.default.contents(atPath: pngImageURL.path)
print(pngImage as Any)
}
}
}