refactor: Move create snapshot to an extension method

This commit is contained in:
Andrea Busi
2022-11-15 22:39:09 +01:00
parent 217b5edbcf
commit beb264f95e
4 changed files with 16 additions and 11 deletions
@@ -137,6 +137,7 @@
65BBB22C26064BE6005D6CDF /* SegnalazioniLast24HoursCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65BBB22B26064BE6005D6CDF /* SegnalazioniLast24HoursCell.swift */; };
65CB83432915720400EE1E35 /* EQNUserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65CB83422915720400EE1E35 /* EQNUserData.swift */; };
65D409942619BA34008CF356 /* SegnalazioniSendReportCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65D409932619BA34008CF356 /* SegnalazioniSendReportCell.swift */; };
65D9938A29219DEC00F2B0EB /* UIKit+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65583A04261B83BE00ECA9F9 /* UIKit+Extensions.swift */; };
65DBFB4B25E29DD60041CBA6 /* SeismicNetworksMapDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65DBFB4A25E29DD60041CBA6 /* SeismicNetworksMapDetailViewController.swift */; };
65DBFB7425E2BBF20041CBA6 /* GADTMediumTemplateView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 65DBFB6F25E2BBF20041CBA6 /* GADTMediumTemplateView.xib */; };
65DBFB7525E2BBF20041CBA6 /* GADTMediumTemplateView.m in Sources */ = {isa = PBXBuildFile; fileRef = 65DBFB7125E2BBF20041CBA6 /* GADTMediumTemplateView.m */; };
@@ -1680,6 +1681,7 @@
653C67E625F3CC8400FE52AC /* EQNCustomAnnotationView.swift in Sources */,
654D18DA25F9424700BB6DB0 /* EQNUtility+Extensions.swift in Sources */,
DC0AE1BA2538204100111307 /* EQNPastquakes.m in Sources */,
65D9938A29219DEC00F2B0EB /* UIKit+Extensions.swift in Sources */,
8CF12CD921DE49B600613AC5 /* NotificationViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -505,16 +505,6 @@ class SeismicNetworkTableViewCell: UITableViewCell {
updateUI()
}
/// Creates a snapshot of the current cell
/// - Returns: Image with the snapshot of the cell
public func createSnapshot() -> UIImage {
let renderer = UIGraphicsImageRenderer(size: contentView.bounds.size)
let image = renderer.image { ctx in
contentView.drawHierarchy(in: contentView.bounds, afterScreenUpdates: true)
}
return image
}
// MARK: - Actions
@objc func shareTapped(_ sender: UIButton) {
@@ -317,7 +317,7 @@ extension SeismicNetworksViewController: SeismicNetworkTableViewCellDelegate {
guard let index = tableView?.indexPath(for: cell), case let .seismic(seismic) = rows[index.row] else { return }
// create a snapshot of the cell and share with default share sheet
let snapshot = cell.createSnapshot()
let snapshot = cell.contentView.createSnapshot()
// text to share with the snapshot
let shareHashtag = NSLocalizedString("share_hashtag", comment: "")
@@ -22,3 +22,16 @@ extension UIButton {
setTitle(title, for: .normal)
}
}
extension UIView {
/// Creates a snapshot of the current view
/// - Returns: Image with the snapshot of the view
public func createSnapshot() -> UIImage {
let renderer = UIGraphicsImageRenderer(size: bounds.size)
let image = renderer.image { ctx in
drawHierarchy(in: bounds, afterScreenUpdates: true)
}
return image
}
}