diff --git a/MediaBrowser/MediaBrowser.swift b/MediaBrowser/MediaBrowser.swift index 6fcab0a..23219a9 100755 --- a/MediaBrowser/MediaBrowser.swift +++ b/MediaBrowser/MediaBrowser.swift @@ -201,10 +201,10 @@ public class MediaBrowser: UIViewController, UIScrollViewDelegate, UIActionSheet /// Caching image count both side (e.g. when index 1, caching 0 and 2) public var cachingImageCount = 1 - /// Caching before MediaBrowser comes up - public var precachingEnabled = false { + /// Caching before MediaBrowser comes up, set + public var preCachingEnabled = false { didSet { - if precachingEnabled { + if preCachingEnabled { startPreCaching() } } @@ -1037,7 +1037,7 @@ public class MediaBrowser: UIViewController, UIScrollViewDelegate, UIActionSheet func startPreCaching() { if let d = delegate { - let media = d.media(for: self, at: 0) + let media = d.media(for: self, at: currentPageIndex) media.loadUnderlyingImageAndNotify() } else { @@ -1064,6 +1064,32 @@ public class MediaBrowser: UIViewController, UIScrollViewDelegate, UIActionSheet } //MARK: - Paging + + /** + setCurrentIndex to show first. + When precaching, set this method first. + + - Parameter index: Int + */ + public func setCurrentIndex(at index: Int) { + var internalIndex = 0 + let mediaCount = self.numberOfMedias + if mediaCount != 0 { + if index >= mediaCount { + internalIndex = self.numberOfMedias - 1 + } else { + internalIndex = index + } + + currentPageIndex = internalIndex + if self.isViewLoaded { + self.jumpToPageAtIndex(index: internalIndex, animated: false) + if !viewIsActive { + self.tilePages() + } + } + } + } func tilePages() { // Calculate which pages should be visible diff --git a/MediaBrowserDemo/ViewController.swift b/MediaBrowserDemo/ViewController.swift index 5aa669a..b61e7e8 100644 --- a/MediaBrowserDemo/ViewController.swift +++ b/MediaBrowserDemo/ViewController.swift @@ -33,8 +33,10 @@ class ViewController: UITableViewController { super.viewDidLoad() - // Set delegate and pre-cahing when you need +// Set delegate and pre-cahing when you need - add current index when you need to caching index // browser = MediaBrowser(delegate: self) +// browser.setCurrentIndex(at: 1) +// // browser.precachingEnabled = true let font = UIFont.systemFont(ofSize: 12) @@ -192,6 +194,7 @@ extension ViewController { browser.startOnGrid = startOnGrid browser.enableSwipeToDismiss = true browser.autoPlayOnAppear = autoPlayOnAppear +// browser.setCurrentIndex(at: 1) if displaySelectionButtons {