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

Added ability to add a fallback image for all ImageSlideshowItem's #184

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
8 changes: 7 additions & 1 deletion ImageSlideshow/Classes/Core/ImageSlideshow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ open class ImageSlideshow: UIView {
return nil
}
}

/// Optional fallbackImage for each InputSource
open var fallbackImage: ImageSource?

/// Current scroll view page. This may differ from `currentPage` as circular slider has two more dummy pages at indexes 0 and n-1 to provide fluent scrolling between first and last item.
open fileprivate(set) var scrollViewPage: Int = 0
Expand Down Expand Up @@ -149,6 +152,9 @@ open class ImageSlideshow: UIView {
}
}
}

// Fallback content mode for fallbackImage
open var fallbackScaleMode: UIViewContentMode = UIViewContentMode.scaleAspectFit

fileprivate var slideshowTimer: Timer?
fileprivate var scrollViewImages = [InputSource]()
Expand Down Expand Up @@ -245,7 +251,7 @@ open class ImageSlideshow: UIView {

var i = 0
for image in scrollViewImages {
let item = ImageSlideshowItem(image: image, zoomEnabled: self.zoomEnabled, activityIndicator: self.activityIndicator?.create())
let item = ImageSlideshowItem(image: image, zoomEnabled: self.zoomEnabled, activityIndicator: self.activityIndicator?.create(), fallbackImage: fallbackImage, fallbackScaleMode: self.fallbackScaleMode)
item.imageView.contentMode = self.contentScaleMode
slideshowItems.append(item)
scrollView.addSubview(item)
Expand Down
21 changes: 19 additions & 2 deletions ImageSlideshow/Classes/Core/ImageSlideshowItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {

/// Input Source for the item
open let image: InputSource

/// Fallback Input Source for the item
open let fallbackImage: InputSource?

/// Scale mode for fallback input source
open let fallbackScaleMode: UIViewContentMode

/// Guesture recognizer to detect double tap to zoom
open var gestureRecognizer: UITapGestureRecognizer?
Expand Down Expand Up @@ -45,11 +51,15 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {
Initializes a new ImageSlideshowItem
- parameter image: Input Source to load the image
- parameter zoomEnabled: holds if it should be possible to zoom-in the image
- parameter fallbackImage: A fallback image to display if image is unavailable
- parameter fallbackScaleMode: The fallback image scale mode
*/
init(image: InputSource, zoomEnabled: Bool, activityIndicator: ActivityIndicatorView? = nil) {
init(image: InputSource, zoomEnabled: Bool, activityIndicator: ActivityIndicatorView? = nil, fallbackImage: InputSource? = nil, fallbackScaleMode: UIViewContentMode = UIViewContentMode.scaleAspectFit) {
self.zoomEnabled = zoomEnabled
self.image = image
self.activityIndicator = activityIndicator
self.fallbackImage = fallbackImage
self.fallbackScaleMode = fallbackScaleMode

super.init(frame: CGRect.null)

Expand Down Expand Up @@ -116,7 +126,8 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {

/// Request to load Image Source to Image View
func loadImage() {
if self.imageView.image == nil && !isLoading {
if (self.imageView.image == nil || self.loadFailed) && !isLoading {
self.imageView.image = nil
isLoading = true
imageReleased = false
activityIndicator?.show()
Expand All @@ -126,6 +137,12 @@ open class ImageSlideshowItem: UIScrollView, UIScrollViewDelegate {
self.activityIndicator?.hide()
self.loadFailed = image == nil
self.isLoading = false
if self.loadFailed && self.fallbackImage != nil {
self.fallbackImage?.load(to: self.imageView) { fallbackImage in
self.imageView.image = self.imageReleased ? nil : fallbackImage
self.imageView.contentMode = self.fallbackScaleMode
}
}
}
}
}
Expand Down