111 lines
3.5 KiB
Swift
Executable File
111 lines
3.5 KiB
Swift
Executable File
//
|
|
// SelectionViewController.swift
|
|
// Project30
|
|
//
|
|
// Created by TwoStraws on 20/08/2016.
|
|
// Copyright (c) 2016 TwoStraws. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
let cellReuseIdentifier = "Cell"
|
|
let cellRowHeight = 90
|
|
|
|
class SelectionViewController: UITableViewController {
|
|
var items = [String]() // this is the array that will store the filenames to load
|
|
var dirty = false
|
|
|
|
lazy var thumbnailRect = CGRect(origin: CGPoint.zero, size: CGSize(width: cellRowHeight, height: cellRowHeight))
|
|
lazy var thumbnailRenderer = UIGraphicsImageRenderer(size: thumbnailRect.size)
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
title = "Reactionist"
|
|
|
|
tableView.rowHeight = CGFloat(cellRowHeight)
|
|
tableView.separatorStyle = .none
|
|
|
|
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
|
|
|
|
// load all the JPEGs into our array
|
|
let fm = FileManager.default
|
|
|
|
if let tempItems = try? fm.contentsOfDirectory(atPath: Bundle.main.resourcePath!) {
|
|
for item in tempItems {
|
|
if item.range(of: "Large") != nil {
|
|
items.append(item)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
|
|
if dirty {
|
|
// we've been marked as needing a counter reload, so reload the whole table
|
|
tableView.reloadData()
|
|
}
|
|
}
|
|
|
|
// MARK: - Table view data source
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
// Return the number of sections.
|
|
return 1
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
// Return the number of rows in the section.
|
|
return items.count * 10
|
|
}
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier)!
|
|
|
|
// find the image for this cell, and load its thumbnail
|
|
let currentImage = items[indexPath.row % items.count]
|
|
let imageRootName = currentImage.replacingOccurrences(of: "Large", with: "Thumb")
|
|
let path = Bundle.main.path(forResource: imageRootName, ofType: nil)!
|
|
let thumbnailSourceImage = UIImage(contentsOfFile: path)!
|
|
|
|
let roundedImage = thumbnailRenderer.image { ctx in
|
|
ctx.cgContext.addEllipse(in: thumbnailRect)
|
|
ctx.cgContext.clip()
|
|
|
|
thumbnailSourceImage.draw(in: thumbnailRect)
|
|
}
|
|
|
|
cell.imageView?.image = roundedImage
|
|
|
|
// give the images a nice shadow to make them look a bit more dramatic
|
|
cell.imageView?.layer.shadowColor = UIColor.black.cgColor
|
|
cell.imageView?.layer.shadowOpacity = 1
|
|
cell.imageView?.layer.shadowRadius = 10
|
|
cell.imageView?.layer.shadowOffset = CGSize.zero
|
|
|
|
// 🔑 Tell iOS not to automatically calculate the shadow path for our images by giving it the exact shadow path to use
|
|
cell.imageView?.layer.shadowPath = UIBezierPath(ovalIn: thumbnailRect).cgPath
|
|
|
|
// each image stores how often it's been tapped
|
|
let defaults = UserDefaults.standard
|
|
cell.textLabel?.text = "\(defaults.integer(forKey: currentImage))"
|
|
|
|
return cell
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
let vc = ImageViewController()
|
|
vc.image = items[indexPath.row % items.count]
|
|
vc.owner = self
|
|
|
|
// mark us as not needing a counter reload when we return
|
|
dirty = false
|
|
|
|
// add to our view controller cache and show
|
|
navigationController!.pushViewController(vc, animated: true)
|
|
}
|
|
}
|