Skip to content

Instantly share code, notes, and snippets.

@CodeNextPaco
Created January 17, 2022 19:46
Show Gist options
  • Save CodeNextPaco/b1ba77adf8316aab7e3d3dc184c6e878 to your computer and use it in GitHub Desktop.
Save CodeNextPaco/b1ba77adf8316aab7e3d3dc184c6e878 to your computer and use it in GitHub Desktop.
Project 3 view controller - code next iOS club
import UIKit
class ViewController: UICollectionViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
var people = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self,
action: #selector(addNewPerson))
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// getting the person that was selected
let person = people[indexPath.item]
let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .alert)
// Adding a text field for the person to input their new name
ac.addTextField()
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel)) // Adding a cancel button
ac.addAction(UIAlertAction(title: "OK", style: .default) { [weak self, weak ac] _ in
guard let newName = ac?.textFields?[0].text else { return }
person.name = newName
self?.collectionView.reloadData()
})
present(ac, animated: true)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return people.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Person", for:
indexPath) as? PersonCell else {
// we failed to get a PersonCell – bail out!
fatalError("Unable to dequeue PersonCell.")
}
let person = people[indexPath.item] // pulls out a person in the correct position
cell.name.text = person.name // sets the name
// gets the right path for the image
let path = getDocumentsDirectory().appendingPathComponent(person.image)
// creates an image from the provided path
cell.imageView.image = UIImage(contentsOfFile: path.path)
// gives it a grey border
cell.imageView.layer.borderColor = UIColor(white: 0, alpha: 0.3).cgColor
cell.imageView.layer.borderWidth = 2
// rounds the corners of the image
cell.imageView.layer.cornerRadius = 3
// rounds the corners of the cell
cell.layer.cornerRadius = 7
// if we're still here it means we got a PersonCell, so we can return it
return cell
}
@objc func addNewPerson(){
let picker = UIImagePickerController()
picker.allowsEditing = true // allows the user to crop the picture they select
picker.delegate = self // Means this class will be handling the picture selection
present(picker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any] ) {
guard let image = info[.editedImage] as? UIImage else { return }
let imageName = UUID().uuidString
let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
if let jpegData = image.jpegData(compressionQuality: 0.8) {
try? jpegData.write(to: imagePath)
}
dismiss(animated: true)
let person = Person(name: "Unknown", image: imageName)
people.append(person)
collectionView.reloadData()
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment