Created
August 3, 2018 02:03
-
-
Save ameseee/dcf34d70853e917970e375ae6d50b319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
import UIKit | |
class AddPhotoViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
let imagePicker = UIImagePickerController() | |
@IBOutlet weak var newImageView: UIImageView! | |
@IBOutlet weak var captionText: UITextField! | |
@IBOutlet weak var emojiIcon: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
imagePicker.delegate = self | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { | |
newImageView.image = selectedImage | |
} | |
imagePicker.dismiss(animated: true, completion: nil) | |
} | |
@IBAction func findPhotoTapped(_ sender: UIBarButtonItem) { | |
imagePicker.sourceType = .photoLibrary | |
present(imagePicker, animated: true, completion: nil) | |
} | |
@IBAction func takePhotoTapped(_ sender: UIBarButtonItem) { | |
imagePicker.sourceType = .camera | |
present(imagePicker, animated: true, completion: nil) | |
} | |
@IBAction func savePhotoTapped(_ sender: UIButton) { | |
if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext { | |
let photoToSave = Photos(entity: Photos.entity(), insertInto: context) | |
photoToSave.caption = captionText.text | |
photoToSave.emojiIcon = emojiIcon.text | |
if let userImage = newImageView.image { | |
if let userImageData = UIImagePNGRepresentation(userImage) { | |
photoToSave.imageData = userImageData | |
} | |
} | |
(UIApplication.shared.delegate as? AppDelegate)?.saveContext() | |
navigationController?.popViewController(animated: true) | |
} | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment