Swiftpack.co - Package - nathan-hh/SwiftyMediaGallery

SwiftyMediaGallery

CI Status Version License Platform Swift

Demo

Requirements

  • [X] 📱 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 images downloading
  • ☑ Support local images (on device)
  • ☑ 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
  • ☑ 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](https://raw.github.com/nathan-hh/SwiftyMediaGallery/blob/master/))
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)

Set-UIImageView

You can enjoy the benefit of the library to set your on UIImageView and have auto downloading and caching

yourImageView.set(image: url, withIndicator: false) //deafult with activity indicator

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

Set options saveToDisk / saveToMemory / clearOnMemoryWarning

Configuration.cacheOptions = [.clearOnMemoryWarning,.saveToDisk,.saveToMemory] // default is all
Configuration.diskCacheMB = 300  // default is 200

You can get the current MB used by the disk cache

print(Configuration.diskCacheMB)

Image rendering max size

You can set it low to save memory and disk cache

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](https://raw.github.com/nathan-hh/SwiftyMediaGallery/blob/master/)

currentIndex.sink { (index) in
    //update VC
}.store(in: &subscriptions)

Configuration

Set gallery navigation title and backgroundColor

Configuration.shared.title = "Best Gallery"
Configuration.shared.backgroundColor = .white 

Configure actions when delete/share button was clicked

GalleryConfiguration.shared.onDelete { (item) in
    deleteItem(id: item.id)
}
GalleryConfiguration.shared.onShare { (item) in
    shareItem(id: item.id)
}

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/nathan-hh/SwiftyMediaGallery.git

Author

nathan-hh, fdg

License

SwiftyMediaGallery is available under the MIT license. See the LICENSE file for more info.

Github

link
Stars: 0

Dependencies

Used By

Total: 0