A comfortable media viewer like the iOS standard.
- iOS 16+
- Swift 5.8+
-
Make a type that conforms to
MediaViewerDataSource
protocol.extension YourViewController: MediaViewerDataSource { // You can specify any type that conforms to `Hashable`. typealias MediaIdentifier = UIImage // var images: [UIImage] func mediaIdentifiers( for mediaViewer: MediaViewerViewController ) -> [MediaIdentifier] { images } func mediaViewer( _ mediaViewer: MediaViewerViewController, mediaWith mediaIdentifier: MediaIdentifier // UIImage ) -> Media { .sync(mediaIdentifier) // Or you can fetch media asynchronously by `.async { ... }` } func mediaViewer( _ mediaViewer: MediaViewerViewController, transitionSourceViewForMediaWith mediaIdentifier: MediaIdentifier ) -> UIView? { // Return a view that is animated when the viewer opens or closes. imageView(for: mediaIdentifier) } }
-
Create a
MediaViewerViewController
instance and push it. That's all! 🎉let mediaViewer = MediaViewerViewController(opening: image, dataSource: self) navigationController?.delegate = mediaViewer navigationController?.pushViewController(mediaViewer, animated: true)
See demo for more detailed usage.
To use the MediaViewer
library in a SwiftPM project, add the following line to the dependencies in your Package.swift
file:
.package(url: "https://github.com/jrsaruo/MediaViewer", from: "0.1.3"),
and add MediaViewer
as a dependency for your target:
.target(name: "<target>", dependencies: [
.product(name: "MediaViewer", package: "MediaViewer"),
// other dependencies
]),
Finally, add import MediaViewer
in your source code.