how to pass inside collection view item to prepare for segue

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhoneDetailsCC", for: indexPath) as? PhoneDetailsCC
    let imageUrl = phoneModel?.imageUrl[indexPath.row]
    cell?.phoneDetailsImage.sd_setImage(with: URL(string: imageUrl!))
    return cell!
  }

i am casting here cell image phoneDetailsImage and i use here sd_setimage to download inside array to come it other tableview each row also every cell to use model let imageUrl = phoneModel?.imageUrl[indexPath.row] this is and string array [string] and sdwebimage show inside url to convert image but i have another problem to it

i am create an pdf in this every cell to use pdfkit beforo i used just 1 image and i prepared and send it other vc but i dont know how i send array of image to other vc i will share other code below

import FirebaseStorage
import FirebaseFirestore
import SDWebImage
import ProgressHUD
import PDFKit


class PhoneListViewController: UITableViewController {

  @IBOutlet weak var phoneModelText: UITextView!
  @IBOutlet weak var imeiAdressText: UITextView!
  @IBOutlet weak var userNameText: UITextView!
  @IBOutlet weak var idText: UITextView!
  @IBOutlet weak var phoneNumberText: UITextView!
  @IBOutlet weak var detailsText: UITextView!
  @IBOutlet weak var dateText: UITextView!
  @IBOutlet weak var priceText: UITextView!
  @IBOutlet weak var adressText: UITextView!
  @IBOutlet weak var imageView: UIImageView!
  @IBOutlet weak var imageCollectionView: UICollectionView!
   
  public var documentData: Data?
  var phoneModel : PhoneModel?

   
   
  override func viewDidLoad() {
    super.viewDidLoad()
    imageCollectionView.delegate = self
    imageCollectionView.dataSource = self
    view.backgroundColor? = UIColor.systemGray3
    tableView.backgroundView = UIImageView(image: UIImage(named: "SplashScreen.jpeg"))
    tableView.backgroundView?.alpha = 0.2
    phoneModelText.text = phoneModel?.phoneModelText
    imeiAdressText.text = phoneModel?.imeiAdressText
    userNameText.text = phoneModel?.userNameText
    idText.text = phoneModel?.idText
    phoneNumberText.text = phoneModel?.phoneNumberText
    detailsText.text = phoneModel?.detailsText
    dateText.text = phoneModel?.dateText
    priceText.text = phoneModel?.priceText
    adressText.text = phoneModel?.adressText
    imageCollectionView.register(UINib(nibName:"PhoneDetailsCC", bundle: Bundle.main), forCellWithReuseIdentifier: "PhoneDetailsCC")

     
  }
   
  @IBAction func printAction(_ sender: Any) {

    let pdfPreview = PDFView()

    if let data = documentData {
      pdfPreview.document = PDFDocument(data: data)
      pdfPreview.autoScales = true
    }
    view.addSubview(pdfPreview)
  }

  override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if
      let _ = phoneModelText.text,
      let _ = userNameText.text,
      let _ = imageView.image, // this is i used before only 1 image and this is worked for me 
      let _ = adressText.text {
      return true
    }
     
    let alert = UIAlertController(title: "Please Wait, Try again", message: "You Need to be wait downloading all image", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    present(alert, animated: true, completion: nil)
     
    return false
  }
   
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == K.pdfSegue {
      guard let vc = segue.destination as? PDFViewController else { return }
       
      if let phoneM = phoneModelText.text,
        let imeiA = imeiAdressText.text,
        let nameS = userNameText.text,
        let idN = idText.text,
        let phoneN = phoneNumberText.text,
        let adressT = adressText.text,
        let detailS = detailsText.text,
        let priceC = priceText.text,
        let dateT = dateText.text,
        let imageV = imageView.image 
       
      {
        let pdfCreate = PDFCreate(phoneModel: phoneM, imeiAdress: imeiA, nameSurname: nameS, id: idN, phoneNumber: phoneN, adress: adressT, details: detailS, price: priceC, date: dateT, image: imageV)
        vc.documentData = pdfCreate.createPdf()
      } //this model use to create of the inside of pdf other text is passing well
    }
  }
}

extension PhoneListViewController: UICollectionViewDelegate,UICollectionViewDataSource {
   
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return phoneModel?.imageUrl.count ?? 0
  }
   
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
    
     
  }
   
  func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
     
   
  }
   
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhoneDetailsCC", for: indexPath) as? PhoneDetailsCC
    let imageUrl = phoneModel?.imageUrl[indexPath.row]
    cell?.phoneDetailsImage.sd_setImage(with: URL(string: imageUrl!))
    return cell!
  }
   
   
}

how i send it array of images to use sdwebimage inside of collection view data to other vc Thank you for answers...

Answered by Claude31 in 709308022

It is hard to understand what you want to do.

.

how i send it array of images

Where is this array defined ? What is its name ?

For example, if it is defined in PhoneListViewController as

var images: [UIImage]

What is the var that will receive the array in the destination controller (PDFViewController ?)

if it is defined as:

var receivedImages: [UIImage]?

Then, in prepare you have to write:

vc.receivedImages = self.images

Note: the forced unwrap at the end on cell! is dangerous.

You should better:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhoneDetailsCC", for: indexPath) as? PhoneDetailsCC else { return UICollectionViewCell() }
    let imageUrl = phoneModel?.imageUrl[indexPath.row]
    cell.phoneDetailsImage.sd_setImage(with: URL(string: imageUrl!))
    return cell
  }
Accepted Answer

It is hard to understand what you want to do.

.

how i send it array of images

Where is this array defined ? What is its name ?

For example, if it is defined in PhoneListViewController as

var images: [UIImage]

What is the var that will receive the array in the destination controller (PDFViewController ?)

if it is defined as:

var receivedImages: [UIImage]?

Then, in prepare you have to write:

vc.receivedImages = self.images

Note: the forced unwrap at the end on cell! is dangerous.

You should better:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhoneDetailsCC", for: indexPath) as? PhoneDetailsCC else { return UICollectionViewCell() }
    let imageUrl = phoneModel?.imageUrl[indexPath.row]
    cell.phoneDetailsImage.sd_setImage(with: URL(string: imageUrl!))
    return cell
  }

firstly Thank you for answering sory for my bad explanation i want to downloaded image inside collection view to pass this vc using pdf create model

import UIKit
import PDFKit

class PDFCreate: NSObject {
  let phoneModel: String
  let imeiAdress: String
  let nameSurname : String
  let id: String
  let phoneNumber : String
  let adress : String
  let details : String
  let price : String
  let date : String
  let image: UIImage
  
  init(phoneModel: String,
     imeiAdress: String,
     nameSurname: String,
     id: String,
     phoneNumber : String,
     adress : String,
     details : String,
     price : String,
     date : String,
     image: UIImage)
  {
    self.phoneModel = phoneModel
    self.imeiAdress = imeiAdress
    self.nameSurname = nameSurname
    self.id = id
    self.phoneNumber = phoneNumber
    self.adress = adress
    self.details = details
    self.price = price
    self.date = date
    self.image = image
  }
   
  func createPdf() -> Data {

    let pdfMetaData = [
      kCGPDFContextCreator: "name",
      kCGPDFContextAuthor: "name,
      kCGPDFContextTitle: phoneModel
    ]
    let format = UIGraphicsPDFRendererFormat()
    format.documentInfo = pdfMetaData as [String: Any]
     
    // 2
    let pageWidth = 1191
    let pageHeight = 1684
    let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)
     
  
    let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
    
    let data = renderer.pdfData { (context) in
      
      context.beginPage()
 
 let imageDraw = CGRect(x: 357, y: 840, width: 665, height: 351)
      image.draw(in: imageDraw) // i want to use there inside model data to array and list of downloaded image to listing there 

}

code line very long that because i share just need it place

       

how to pass inside collection view item to prepare for segue
 
 
Q