|
| 1 | +import AVFoundation |
| 2 | +import ImageIO |
| 3 | +import QuickLook |
| 4 | +import UIKit |
| 5 | +import UniformTypeIdentifiers |
| 6 | + |
| 7 | +private final class FilePreviewItem: NSObject, QLPreviewItem { |
| 8 | + var previewItemURL: URL? |
| 9 | + var previewItemTitle: String? |
| 10 | +} |
| 11 | + |
| 12 | +private struct PreparedPreviewFile { |
| 13 | + let url: URL |
| 14 | + let image: UIImage? |
| 15 | +} |
| 16 | + |
| 17 | +/// UIKit supplies pinch/pan physics, header controls, sharing, and interactive zoom dismissal. |
| 18 | +private final class NativeImagePreviewController: UIViewController, UIScrollViewDelegate { |
| 19 | + var usesZoomTransition = false |
| 20 | + private let imageView: UIImageView |
| 21 | + private let scrollView = UIScrollView() |
| 22 | + private let url: URL |
| 23 | + private let onClose: () -> Void |
| 24 | + private let onDismiss: () -> Void |
| 25 | + private var viewportSize = CGSize.zero |
| 26 | + |
| 27 | + init(image: UIImage, url: URL, title: String, onClose: @escaping () -> Void, onDismiss: @escaping () -> Void) { |
| 28 | + self.imageView = UIImageView(image: image) |
| 29 | + self.url = url |
| 30 | + self.onClose = onClose |
| 31 | + self.onDismiss = onDismiss |
| 32 | + super.init(nibName: nil, bundle: nil) |
| 33 | + self.title = title |
| 34 | + } |
| 35 | + |
| 36 | + required init?(coder: NSCoder) { nil } |
| 37 | + |
| 38 | + override func viewDidLoad() { |
| 39 | + super.viewDidLoad() |
| 40 | + view.backgroundColor = .black |
| 41 | + scrollView.frame = view.bounds |
| 42 | + scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 43 | + scrollView.contentInsetAdjustmentBehavior = .never |
| 44 | + scrollView.showsHorizontalScrollIndicator = false |
| 45 | + scrollView.showsVerticalScrollIndicator = false |
| 46 | + scrollView.delegate = self |
| 47 | + scrollView.accessibilityIdentifier = "ImagePreviewScrollView" |
| 48 | + imageView.isAccessibilityElement = true |
| 49 | + imageView.accessibilityLabel = title |
| 50 | + imageView.accessibilityIdentifier = "ImagePreviewImage" |
| 51 | + scrollView.addSubview(imageView) |
| 52 | + view.addSubview(scrollView) |
| 53 | + |
| 54 | + navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: UIAction { [weak self] _ in |
| 55 | + self?.onClose() |
| 56 | + }) |
| 57 | + navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(share)) |
| 58 | + navigationItem.rightBarButtonItem?.accessibilityLabel = "Share" |
| 59 | + let appearance = UINavigationBarAppearance() |
| 60 | + appearance.configureWithTransparentBackground() |
| 61 | + appearance.titleTextAttributes = [.foregroundColor: UIColor.white] |
| 62 | + navigationItem.standardAppearance = appearance |
| 63 | + navigationItem.scrollEdgeAppearance = appearance |
| 64 | + navigationItem.compactAppearance = appearance |
| 65 | + navigationController?.navigationBar.tintColor = .white |
| 66 | + |
| 67 | + let doubleTap = UITapGestureRecognizer(target: self, action: #selector(zoom)) |
| 68 | + doubleTap.numberOfTapsRequired = 2 |
| 69 | + let singleTap = UITapGestureRecognizer(target: self, action: #selector(toggleControls)) |
| 70 | + singleTap.require(toFail: doubleTap) |
| 71 | + scrollView.addGestureRecognizer(singleTap) |
| 72 | + scrollView.addGestureRecognizer(doubleTap) |
| 73 | + if !usesZoomTransition { |
| 74 | + let swipe = UISwipeGestureRecognizer(target: self, action: #selector(closeIfNotZoomed)) |
| 75 | + swipe.direction = .down |
| 76 | + scrollView.addGestureRecognizer(swipe) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + override func viewDidLayoutSubviews() { |
| 81 | + super.viewDidLayoutSubviews() |
| 82 | + let size = scrollView.bounds.size |
| 83 | + guard size != viewportSize, let image = imageView.image, size.width > 0, size.height > 0 else { return } |
| 84 | + viewportSize = size |
| 85 | + scrollView.minimumZoomScale = 1 |
| 86 | + scrollView.zoomScale = 1 |
| 87 | + imageView.frame = CGRect(origin: .zero, size: image.size) |
| 88 | + scrollView.contentSize = image.size |
| 89 | + let fit = min(size.width / image.size.width, size.height / image.size.height) |
| 90 | + scrollView.maximumZoomScale = max(fit * 4, 1) |
| 91 | + scrollView.minimumZoomScale = fit |
| 92 | + scrollView.zoomScale = fit |
| 93 | + centerImage() |
| 94 | + } |
| 95 | + |
| 96 | + override func viewDidDisappear(_ animated: Bool) { |
| 97 | + super.viewDidDisappear(animated) |
| 98 | + if isBeingDismissed || navigationController?.isBeingDismissed == true || navigationController?.presentingViewController == nil { |
| 99 | + onDismiss() |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + func viewForZooming(in scrollView: UIScrollView) -> UIView? { imageView } |
| 104 | + func scrollViewDidZoom(_ scrollView: UIScrollView) { centerImage() } |
| 105 | + |
| 106 | + private func centerImage() { |
| 107 | + let horizontal = max(0, (scrollView.bounds.width - scrollView.contentSize.width) / 2) |
| 108 | + let vertical = max(0, (scrollView.bounds.height - scrollView.contentSize.height) / 2) |
| 109 | + scrollView.contentInset = UIEdgeInsets(top: vertical, left: horizontal, bottom: vertical, right: horizontal) |
| 110 | + } |
| 111 | + |
| 112 | + @objc private func zoom(_ recognizer: UITapGestureRecognizer) { |
| 113 | + if scrollView.zoomScale > scrollView.minimumZoomScale * 1.01 { |
| 114 | + scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true) |
| 115 | + } else { |
| 116 | + let scale = min(scrollView.minimumZoomScale * 3, scrollView.maximumZoomScale) |
| 117 | + let point = recognizer.location(in: imageView) |
| 118 | + let size = CGSize(width: scrollView.bounds.width / scale, height: scrollView.bounds.height / scale) |
| 119 | + let rect = CGRect(x: point.x - size.width / 2, y: point.y - size.height / 2, width: size.width, height: size.height) |
| 120 | + scrollView.zoom(to: rect, animated: true) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @objc private func toggleControls() { |
| 125 | + guard let navigationController else { return } |
| 126 | + navigationController.setNavigationBarHidden(!navigationController.isNavigationBarHidden, animated: true) |
| 127 | + } |
| 128 | + |
| 129 | + @objc private func closeIfNotZoomed() { |
| 130 | + if scrollView.zoomScale <= scrollView.minimumZoomScale * 1.01 { onClose() } |
| 131 | + } |
| 132 | + |
| 133 | + @objc private func share(_ sender: UIBarButtonItem) { |
| 134 | + let sheet = UIActivityViewController(activityItems: [url], applicationActivities: nil) |
| 135 | + sheet.popoverPresentationController?.barButtonItem = sender |
| 136 | + present(sheet, animated: true) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// Shares file preparation and lifecycle between the image viewer and Quick Look documents. |
| 141 | +final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource, |
| 142 | + QLPreviewControllerDelegate, UIAdaptivePresentationControllerDelegate { |
| 143 | + let identifier: String |
| 144 | + private var controller: UIViewController? |
| 145 | + private let completion: (Error?) -> Void |
| 146 | + private weak var sources: T3PresentationSources? |
| 147 | + private let sourceIdentifier: String |
| 148 | + private let item = FilePreviewItem() |
| 149 | + private var loading: Task<Void, Never>? |
| 150 | + private var presented = false |
| 151 | + private var dismissRequested = false |
| 152 | + private var finished = false |
| 153 | + |
| 154 | + init(identifier: String, sources: T3PresentationSources, sourceIdentifier: String, completion: @escaping (Error?) -> Void) { |
| 155 | + self.identifier = identifier |
| 156 | + self.sources = sources |
| 157 | + self.sourceIdentifier = sourceIdentifier |
| 158 | + self.completion = completion |
| 159 | + super.init() |
| 160 | + } |
| 161 | + |
| 162 | + func present(url: URL, title: String, from presenter: UIViewController) { |
| 163 | + loading = Task { @MainActor [self] in |
| 164 | + do { |
| 165 | + let file = try await Self.prepareFile(url: url, title: title) |
| 166 | + guard !finished, !Task.isCancelled else { |
| 167 | + try? FileManager.default.removeItem(at: file.url.deletingLastPathComponent()) |
| 168 | + return |
| 169 | + } |
| 170 | + item.previewItemURL = file.url |
| 171 | + item.previewItemTitle = title |
| 172 | + let preview: UIViewController |
| 173 | + if let image = file.image { |
| 174 | + let viewer = NativeImagePreviewController( |
| 175 | + image: image, url: file.url, title: title, |
| 176 | + onClose: { [weak self] in self?.dismiss() }, onDismiss: { [weak self] in self?.finish() } |
| 177 | + ) |
| 178 | + let navigation = UINavigationController(rootViewController: viewer) |
| 179 | + navigation.modalPresentationStyle = .fullScreen |
| 180 | + navigation.overrideUserInterfaceStyle = .dark |
| 181 | + preview = navigation |
| 182 | + if #available(iOS 18.0, *), !UIAccessibility.isReduceMotionEnabled, |
| 183 | + sources?.view(for: sourceIdentifier)?.window != nil { |
| 184 | + viewer.usesZoomTransition = true |
| 185 | + let options = UIViewController.Transition.ZoomOptions() |
| 186 | + options.alignmentRectProvider = { context in |
| 187 | + AVMakeRect(aspectRatio: image.size, insideRect: context.zoomedViewController.view.bounds) |
| 188 | + } |
| 189 | + preview.preferredTransition = .zoom(options: options) { [weak self] _ in |
| 190 | + guard let self, let source = sources?.view(for: sourceIdentifier), source.window != nil else { return nil } |
| 191 | + return source |
| 192 | + } |
| 193 | + } |
| 194 | + navigation.view.backgroundColor = .black |
| 195 | + } else { |
| 196 | + let quickLook = QLPreviewController() |
| 197 | + quickLook.delegate = self |
| 198 | + quickLook.dataSource = self |
| 199 | + preview = quickLook |
| 200 | + } |
| 201 | + controller = preview |
| 202 | + presenter.present(preview, animated: !UIAccessibility.isReduceMotionEnabled) { [self] in |
| 203 | + presented = true |
| 204 | + if dismissRequested { dismiss() } |
| 205 | + } |
| 206 | + preview.presentationController?.delegate = self |
| 207 | + } catch { |
| 208 | + finish(error: error) |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + func dismiss() { |
| 214 | + dismissRequested = true |
| 215 | + loading?.cancel() |
| 216 | + guard !finished else { return } |
| 217 | + guard presented else { |
| 218 | + if controller?.presentingViewController == nil { finish() } |
| 219 | + return |
| 220 | + } |
| 221 | + controller?.dismiss(animated: !UIAccessibility.isReduceMotionEnabled) { [self] in finish() } |
| 222 | + } |
| 223 | + |
| 224 | + func numberOfPreviewItems(in controller: QLPreviewController) -> Int { item.previewItemURL == nil ? 0 : 1 } |
| 225 | + |
| 226 | + func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { |
| 227 | + item |
| 228 | + } |
| 229 | + |
| 230 | + func previewControllerDidDismiss(_ controller: QLPreviewController) { finish() } |
| 231 | + |
| 232 | + func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { finish() } |
| 233 | + |
| 234 | + private func finish(error: Error? = nil) { |
| 235 | + guard !finished else { return } |
| 236 | + finished = true |
| 237 | + loading?.cancel() |
| 238 | + loading = nil |
| 239 | + if let file = item.previewItemURL { |
| 240 | + try? FileManager.default.removeItem(at: file.deletingLastPathComponent()) |
| 241 | + } |
| 242 | + item.previewItemURL = nil |
| 243 | + completion(error) |
| 244 | + } |
| 245 | + |
| 246 | + /// Copy original bytes so preview and sharing do not mutate a draft or workspace file. |
| 247 | + nonisolated private static func prepareFile(url: URL, title: String) async throws -> PreparedPreviewFile { |
| 248 | + try Task.checkCancellation() |
| 249 | + let directory = FileManager.default.temporaryDirectory.appendingPathComponent("t3-preview-\(UUID().uuidString)") |
| 250 | + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) |
| 251 | + do { |
| 252 | + let download = directory.appendingPathComponent("original") |
| 253 | + if url.isFileURL { |
| 254 | + try FileManager.default.copyItem(at: url, to: download) |
| 255 | + } else if url.scheme == "data" { |
| 256 | + try Data(contentsOf: url).write(to: download, options: .atomic) |
| 257 | + } else { |
| 258 | + guard ["https", "http"].contains(url.scheme?.lowercased() ?? "") else { |
| 259 | + throw URLError(.unsupportedURL) |
| 260 | + } |
| 261 | + let (temporaryFile, response) = try await URLSession.shared.download(from: url) |
| 262 | + guard let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode) else { |
| 263 | + throw URLError(.badServerResponse) |
| 264 | + } |
| 265 | + try FileManager.default.moveItem(at: temporaryFile, to: download) |
| 266 | + } |
| 267 | + try Task.checkCancellation() |
| 268 | + let type: UTType |
| 269 | + var previewImage: UIImage? |
| 270 | + if let image = CGImageSourceCreateWithURL(download as CFURL, nil), |
| 271 | + CGImageSourceGetCount(image) > 0, let imageType = CGImageSourceGetType(image), |
| 272 | + let detectedType = UTType(imageType as String) { |
| 273 | + type = detectedType |
| 274 | + // Animated images stay in Quick Look; static images are decoded before the zoom starts. |
| 275 | + if CGImageSourceGetCount(image) == 1 { |
| 276 | + previewImage = UIImage(contentsOfFile: download.path)?.preparingForDisplay() |
| 277 | + } |
| 278 | + } else if CGPDFDocument(download as CFURL) != nil { |
| 279 | + type = .pdf |
| 280 | + } else { |
| 281 | + throw URLError(.cannotDecodeContentData) |
| 282 | + } |
| 283 | + let filename = URL(fileURLWithPath: title).lastPathComponent as NSString |
| 284 | + let originalExtension = filename.pathExtension |
| 285 | + let fileExtension = UTType(filenameExtension: originalExtension) == type |
| 286 | + ? originalExtension : type.preferredFilenameExtension ?? "png" |
| 287 | + let stem = filename.deletingPathExtension |
| 288 | + var name = String(stem.prefix(60)).components(separatedBy: .controlCharacters).joined(separator: "_") |
| 289 | + while name.utf8.count > 200 { name.removeLast() } |
| 290 | + let file = directory.appendingPathComponent("\(name.isEmpty ? "Preview" : name).\(fileExtension)") |
| 291 | + try FileManager.default.moveItem(at: download, to: file) |
| 292 | + return PreparedPreviewFile(url: file, image: previewImage) |
| 293 | + } catch { |
| 294 | + try? FileManager.default.removeItem(at: directory) |
| 295 | + throw error |
| 296 | + } |
| 297 | + } |
| 298 | +} |
0 commit comments