Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a preloaded range #408

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions SKPhotoBrowser/SKPagingScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,24 @@ class SKPagingScrollView: UIScrollView {
recycledPages.removeAll()
}

func loadAdjacentPhotosIfNecessary(_ photo: SKPhotoProtocol, currentPageIndex: Int) {
func loadAdjacentPhotosIfNecessary(_ photo: SKPhotoProtocol, currentPageIndex: Int, preLoadNum: Int = 1, nextLoadNum: Int = 1) {
guard let browser = browser, let page = pageDisplayingAtPhoto(photo) else {
return
}
let pageIndex = (page.tag - pageIndexTagOffset)
if currentPageIndex == pageIndex {
// Previous
if pageIndex > 0 {
let previousPhoto = browser.photos[pageIndex - 1]
for i in max(pageIndex - preLoadNum, 0)...(pageIndex + nextLoadNum) {
guard i < numberOfPhotos else {
break
}
guard pageIndex != i else {
continue
}
let previousPhoto = browser.photos[i]
if previousPhoto.underlyingImage == nil {
previousPhoto.loadUnderlyingImageAndNotify()
}
}
// Next
if pageIndex < numberOfPhotos - 1 {
let nextPhoto = browser.photos[pageIndex + 1]
if nextPhoto.underlyingImage == nil {
nextPhoto.loadUnderlyingImageAndNotify()
}
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion SKPhotoBrowser/SKPhotoBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ open class SKPhotoBrowser: UIViewController {
// open function
open var currentPageIndex: Int = 0
open var initPageIndex: Int = 0
open var preLoadNum: Int = 1
open var nextLoadNum: Int = 1
open var activityItemProvider: UIActivityItemProvider?
open var photos: [SKPhotoProtocol] = []

Expand Down Expand Up @@ -181,7 +183,10 @@ open class SKPhotoBrowser: UIViewController {
}

open func loadAdjacentPhotosIfNecessary(_ photo: SKPhotoProtocol) {
pagingScrollView.loadAdjacentPhotosIfNecessary(photo, currentPageIndex: currentPageIndex)
pagingScrollView.loadAdjacentPhotosIfNecessary(photo,
currentPageIndex: currentPageIndex,
preLoadNum: preLoadNum,
nextLoadNum: nextLoadNum)
}

// MARK: - initialize / setup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class FromWebViewController: UIViewController, SKPhotoBrowserDelegate {
let browser = SKPhotoBrowser(photos: createWebPhotos())
browser.initializePageIndex(0)
browser.delegate = self
browser.preLoadNum = 10
browser.nextLoadNum = 10

present(browser, animated: true, completion: nil)
}
Expand Down