I have a TableviewController which has multiple customs cells. One of them has a CollectionView (inside the tableView cell). In the CollectionView Cell I have as Image View where I need to display a map. Everything works but when I navigate to the TableviewController for the first time and scroll down I get a glitch/lag/frame drop at a certain spot. this only happens once and if you go back to the previous view then again to the TableView Controller the lag doesn't seem to be present.
If I don't set the map image then the lag goes away so this is definitely causing that issue.
// TableView cellForRowAt
case .branchesCell:
let cell = tableView.dequeueReusableCell(withIdentifier: "branchesCell", for: indexPath) as! BranchesCell
cell.branches = restaurant?.branches
cell.restaurantName = restaurant?.metadata.name ?? ""
return cell
// Tableview Cell Class - BranchesCell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NestledBranchCell
cell.layer.masksToBounds = false
cell.backgroundColor = .clear
if let branches = branches {
cell.branch = branches[indexPath.row]
cell.restaurantName = restaurantName
}
return cell
}
// Collectionview Cell Class - NestledBranchCell
override func awakeFromNib() {
super.awakeFromNib()
contentView.layer.applySketchShadow(color: .black, alpha: 0.14, x: 0, y: 7, blur: 14, spread: 0)
containerView.clipsToBounds = true
containerView.layer.cornerRadius = 15
tradingStatusLbl.layer.cornerRadius = 10
tradingStatusLbl.layer.masksToBounds = true
directionsBtn.layer.cornerRadius = 10
callBtn.layer.cornerRadius = 10
}
override func draw(_ rect: CGRect) {
super.draw(rect)
setupCell() // Setting a few labels
Task {
await setupMap()
}
}
func setupMap() async {
if let branch = branch {
let mapSnapshotOptions = MKMapSnapshotter.Options()
let location = branch.coordinates.coordinate
let region = MKCoordinateRegion(center: location, latitudinalMeters: 300, longitudinalMeters: 300)
mapSnapshotOptions.region = region
mapSnapshotOptions.scale = UIScreen.main.scale
let size = CGSize(width: 314, height: 195)
mapSnapshotOptions.size = size
mapSnapshotOptions.showsBuildings = true
mapSnapshotOptions.pointOfInterestFilter = .none
let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
snapShotter.start { snapshot, error in
let image = snapshot?.image
self.imageView.image = image
}
}
}