I have a simple app where the user taps on a button to bring up a camera, he then takes a picture and I want that picture to then be appended to an array of UIImages objects. I have made a version which I am not sure if it works. Bellow is the code I have written:
mainPictureVC:
import UIKit
import RealmSwift
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// var realm: Realm!
var photoArray = [PhotoArray]()
@IBOutlet weak var imageView: UIImageView!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
// realm = try! Realm()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// let imageToUse = PhotoArray()
// let data = UIImagePNGRepresentation(userPickedImage) //here convert to data
PhotoArray().photosArray.append(userPickedImage) //append converted data in array
imageView.image = userPickedImage
}
// print(PhotoArray().photosArray.count)
imagePicker.dismiss(animated: true, completion: nil)
}
@IBAction func cameraButtonPressed(_ sender: UIButton) {
present(imagePicker, animated: true, completion: nil)
}
}
I attempt to prove that I am saving the images by displaying on a tableView controller but it does not work. (i don't get an error but they just don't apear) TableViewController:
import UIKit
class TableViewController: UITableViewController {
//TableView datasource methods
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// cell.textLabel?.text = categoryArray[indexPath.row].name
// cell.imageView.image = [UIImage, imageNamed: PhotoArray().photosArray.name];
cell.imageView?.image = PhotoArray().photosArray as? UIImage
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let numberOfRowsInSection = PhotoArray().photosArray
return numberOfRowsInSection.count
}
}
PhotoArray DataModel:
import Foundation
import UIKit
class PhotoArray: ViewController {
// var photosArray: [Data] = []
var photosArray: [UIImage] = []
// var photosArray: [UIImage] = [UIImage(named: "")!]
}