I've taken the steps you have suggested, the same error continues on the same line. The viewController calling the function;
import Foundation
import UIKit
import Photos
class PhotoPopUpViewController: UIViewController {
let photoViewController = PhotoViewController()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true, completion: nil)
}
@IBOutlet private var photoCollectionView: UICollectionView!
@IBOutlet weak var tableView: UITableView!
var imageArray = [UIImage]()
var tableImages: [PhotoPopUpCellData] = []
var selectedTableImage: PhotoPopUpCellData?
override func viewDidLoad() {
super.viewDidLoad()
grabPhotos()
tableImages = createArray()
tableView.delegate = self
tableView.dataSource = self
}
func grabPhotos() {
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult: PHFetchResult<PHAsset> = PHAsset.fetchAssets(with: .image, options: fetchOptions)
if fetchResult.count > 0 {
for i in 0..<fetchResult.count {
imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image, error in
self.imageArray.append(image!)
})
}
} else {
print("You got no photos!")
}
}
}
extension PhotoPopUpViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoPopUpCell", for: indexPath) as? DataCollectionViewCell
cell?.img.image = imageArray[indexPath.row]
return cell!
}
}
extension PhotoPopUpViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width / 3.0
let height = collectionView.bounds.height
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1
}
}
extension PhotoPopUpViewController {
func createArray() -> [PhotoPopUpCellData] {
var tempData: [PhotoPopUpCellData] = []
let tableImages1 = PhotoPopUpCellData(image: UIImage(systemName: "photo")!, title: "Open from Albums")
let tableImages2 = PhotoPopUpCellData(image: UIImage(systemName: "camera.fill")!, title: "Take a Photo")
let tableImages3 = PhotoPopUpCellData(image: UIImage(systemName: "tray.and.arrow.up.fill")!, title: "Open last Image")
tempData.append(tableImages1)
tempData.append(tableImages2)
tempData.append(tableImages3)
return tempData
}
}
extension PhotoPopUpViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableImages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableImage = tableImages[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "PhotoPopUpCell") as! PhotoPopUpCell
cell.setPhotoPopUp(photoPopUpCellData: tableImage)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tableImage = tableImages[indexPath.row]
selectedTableImage = tableImage
if indexPath.row == 0 {
present(photoViewController, animated: false, completion: {
self.photoViewController.chooseImage(self)
})
}
if indexPath.row == 1 {
present(photoViewController, animated: false, completion: {
self.photoViewController.accessVideo(self)
})
}
if indexPath.row == 2 {
present(photoViewController, animated: false, completion: {
self.photoViewController.chooseLastImage(self)
})
}
}
}