-1

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: "")!]
}
J. Hoe
  • 53
  • 1
  • 10
  • in your tableView controller, you're getting a new instance of your `PhotoArray` `PhotoArray().photosArray` which is of course empty. You should or use a singleton or pass the `PhotoArray` content to your tableView controller – GIJOW Aug 14 '18 at 17:23

1 Answers1

0

Change your PhotoArray class to a Singleton.

class PhotoArray {
  static let sharedInstance = PhotoArray()
  var photosArray: [UIImage] = []
}

Replace all PhotoArray() to PhotoArray.sharedInstance both the controller and run the code.

Hope it helps you to solve your problem.

Bmacin
  • 235
  • 1
  • 12
  • I did this PhotoArray.sharedInstance.photosArray, for all instances of PhotoArray() in my code. – J. Hoe Aug 14 '18 at 18:20