SwiftyMediaGallery
Demo
Requirements
-
📱 iOS 13 or later
Table of Contents
Description
Showing images and videos in an application is a requirement these days in many apps,
after strageling finding a good library who do it same as the native iOS gallery i've decided to come with a solution myself and help others.
with SwiftyMediaGallery
it is very easy to implement.
The library comes with flexible public API based POP (Protocol Oriented Proggraming) and
a bunch of built-in features:
- Asynchronous image downloading
- Rendering and decoding images on the global thread (default iOS do it on the Main)
- Automatic disk / Memory cache (for now using NSCache)
- Navigation Image Transition animation
- Follow back current image index
- Supports image / video by native + an Open API for adding more items
- Automatic generated videos tumbnails if nedded
Usage
Using 'Combine' library you just need to pass CurrentValueSubject array of AnyMediaItem and init controller with the model
import Combine
let arrMediaItems = CurrentValueSubject <[AnyMediaItem], Never>([AnyMediaItem]())
let currentIndex = CurrentValueSubject <Int, Never>(0)
let viewController = MediaGalleryVC.initVC(array: arrMediaItems, currentIndex: currentIndex)
Create type-erasing items by using wrapper 'AnyMediaItem' and append to an array
let imageItem = ImageMediaItem(url: url.absoluteString)
let videoItem = VideoMediaItem(url: url.absoluteString)
let arr : [AnyMediaItem] = [AnyMediaItem(imageItem),AnyMediaItem(videoItem)]
Then update CurrentValueSubject Array
arrMediaItems.send(arr)
Use placeholder
Placeholder is optional if you want the user to see something while images are being fetched.
let placeholderVideo = UIImage(named: "PlaceholderImage")
let placeholderImage = UIImage(named: "PlaceholderVideo")
Configuration.setVideo(placeholder: placeholderVideo)
Configuration.setImage(placeholder: placeholderImage)
Advanced
Set Navigation Image Transition
viewController.setTransitionConfiguration(from: self, referenceImageView: {[weak self] in
return self!.currentImage
}, referenceImageViewFrameInTransitioningView: { [weak self] in
self!.view.layoutIfNeeded()
return self!.view.convert(self!.currentImage.frame, to: self!.view)
})
navigationController?.pushViewController(viewController, animated: true)
Pre-fetching
Auto pre-fetching when scrolling
Configuration.prefetchUrls = 10 // default is 0
Manually pre-fetch urls
Prefetcher.startFetching(urls:urls)
Prefetcher.cancelFetching(urls:urls)
Caching
In Memory
Configuration.maxMemoryCacheMB = 100 // default is 10
On Disk
Configuration.maxDiskCacheMB = 300 // default is 200
Auto cache video on disk (you must increase cache size)
Configuration.shouldCacheVideo = true // default is false
Image rendering max size
Configuration.imageRendererMaxSize = CGSize(width: 2400,height: 2400) // default is 1200*1200
Show items from a specific index
for example gallery jump and show from 10th picture index
just update currentIndex
currentIndex.send(10)
You can also follow back the index to the original View Controller
private var subscriptions = [AnyCancellable]()
currentIndex.sink { (index) in
//update VC
}.store(in: &subscriptions)
Configuration
Set gallery navigation title and backgroundColor
Configuration.title = "Best Gallery"
Configuration.backgroundColor = .white
Installation
CocoaPods
SwiftyMediaGallery is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftyMediaGallery'
Swift Package Manager
You can also use Swift Package Manager to add SwiftyMediaGallery as a dependency to your project. In order to do so, use the following URL:
https://github.com/arturdev/Unrealm.git
Author
nathan-hh, fdg
License
SwiftyMediaGallery is available under the MIT license. See the LICENSE file for more info.