Skip to content

gokulnair2001/MediaBrowser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📽️ MediaBrowser

MediaBrowser is a data type impartial browser which renders and caches all* the types media

MediaBrowser Architecture

Media Browser currently supports 5 types of Media rendering

  • Image via UIImage
  • Image via URL
  • Video
  • Documents (PDF, docs etc)
  • WebViews

Mechanism

The ideal process of Media Browser happens the following way:

Basic Mechanism

📒 How to Use?

When we need to render any form of media we primarily have three data formats

  • URL
  • Data
  • UIImage

Since Media Browser only understands MediaBrowsable type data, we need to convert the raw data to required type

To perform this operation there are extensions written:

// MARK: - Raw Data Converters
extension MediaBrowserUtils {
    
    /// URL String helper to convert any url to MediaBrowsable type
    public static func mediaBrowsable(_ urlString: String, holderImage: UIImage? = nil) -> MediaBrowsable {
        return MBMediaUrl(url: urlString, placeHolderImage: holderImage)
    }

    
    /// Raw Data helper to convert any data to MediaBrowsable type
    public static func mediaBrowsable(_ data: Data, holderImage: UIImage? = nil) -> MediaBrowsable {
        return MBImageData(data: data, placeHolderImage: holderImage)
    }

    
    /// UIImage helper to convert any image to MediaBrowsable type
    public static func mediaBrowsable(_ image: UIImage, mediaId: String? = nil, holderImage: UIImage? = nil) -> MediaBrowsable {
        return MBImage(id: mediaId, image: image, placeHolderImage: holderImage)
    }
}

Once you have converted raw data to MediaBrowsable type, then use the render() method of media browser to render media.

   let vc = MediaBrowser(storagePolicy: .UsingNSCache)
   let rawUrl = ["https://prod-powerplay.s3.ap-south-1.amazonaws.com/others/PP_1702387271203.png" ,
   "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy-pdf"]
   vc.render (media: rawUr1.map({ SO.mediaBrowsable(placeHolderImage: UIImage(named: "error_504") }))
   vc.popoverPresentationController?.sourceView = self.view
   vc.modalPresentationStyle = .fullScreen
   self.present(vc, animated: true)

Now while initialising MediaBrowser, you need to specify a storage policy to media browser. Available storage policies are:

  1. InMemory

InMemory storage policy stores/caches the rendered media in Browser class. Thus the data will get disposed when Media Browser session is ended.

Such kind of policy is used when temporary browsing is required. Here data is captured till the browser session.

  1. UsingNSCache

UsingNSCache storage policy caches the rendered media for entire app life cycle. Thus the data will get disposed when the app is killed.

Such kind of policy is used when there is frequent demand of media browsing required.

  1. DiskStorage

DiskStorage storage policy caches the rendered media until disk gets out of space or the disk is not cleared. Thus the data will not get disposed even when the app is killed.

Such kind of policy is best used when there are constant images to be rendered repetitively.

By Default InMemory is set as storage policy

🚀 Utility Methods

  1. Placeholder

You can set custom placeholder image for browser

    /// Setting default image for all browsers
    /// - Parameter placeHolderImage: Place holder UIImage for the browser
    public func set(placeHolderImage: UIImage) {
        self.placeHolderImage = placeHolderImage
    }

Releases

No releases published

Packages

No packages published

Languages